285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
Note: If the given node has no in-order successor in the tree, returnnull.
Example 1:
Input:
root = [2,1,3], p = 1
2
/ \
1 3
Output: 2Example 2:
Input:
root = [5,3,6,2,4,null,null,1], p = 6
5
/ \
3 6
/ \
2 4
/
1
Output:nullThoughts: Utilize the binary search property of BST: during the traversal: if a node is about to traverse to its left. Then record the node as successor before doing so.
Code: T: O(h) -> O(logn)
Python
C++
Java
Previous109. Convert Sorted List to Binary Search TreeNextConvert Binary Search Tree (BST) to Sorted Doubly-Linked List
Last updated
Was this helpful?