You are given the root of a binary search tree (BST) and a node p
within the tree. Your task is to find the inorder successor of p
in the BST. The inorder successor of a node p
is defined as the node with the smallest key greater than p.val
.
If the given node p
has no inorder successor (i.e., it is the largest node in the BST), return null
.
For example, consider the following BST:
5
/ \
3 6
/ \
2 4
/
1
If p
is the node with value 3, the inorder successor is the node with value 4.
If p
is the node with value 6, the inorder successor is null
.
If p
is the node with value 4, the inorder successor is the node with value 5.
Clarifications:
p
, not just its value. This means you can directly access the node object.null
.Write a function that takes the root of the BST and the node p
as input and returns the inorder successor of p
.
Given the root of a binary search tree (BST) and a node p
in it, find the in-order successor of that node in the BST. If the given node has no in-order successor in the tree, return null
.
A node q
is the in-order successor of a node p
if q
is the node with the smallest key greater than p.val
.
One straightforward approach is to perform an in-order traversal of the BST and store the nodes in a list. Then, iterate through the list to find the node p
and return the next node in the list. If p
is the last node in the list, return null
.
p
.p
is found and is not the last node in the list, return the next node.null
.def inorder_successor_naive(root, p):
inorder_list = []
def inorder_traversal(node):
if not node:
return
inorder_traversal(node.left)
inorder_list.append(node)
inorder_traversal(node.right)
inorder_traversal(root)
for i in range(len(inorder_list) - 1):
if inorder_list[i] == p:
return inorder_list[i+1]
return None
Since it's a BST, we can improve efficiency by leveraging BST properties. We don't need to traverse the entire tree. The in-order successor must be the smallest node that is greater than the target node. We can traverse the tree, keeping track of the potential successor.
successor
to null
.p.val
, it could be the successor. Update successor
to the current node and go to the left subtree.p.val
, the successor must be in the right subtree, so go to the right subtree.null
.successor
.def inorder_successor(root, p):
successor = None
curr = root
while curr:
if curr.val > p.val:
successor = curr
curr = curr.left
else:
curr = curr.right
return successor
p
is the largest node in the BST, its in-order successor does not exist, and the algorithm should return null
.null
.p
is not in the BST, the algorithm should return the smallest node that is greater than p.val
, or null
if no such node exists.