Given the root of a binary search tree and a node p in it, return the in-order successor of that node in the BST. If the given node has no in-order successor in the tree, return null.
The successor of a node p is the node with the smallest key greater than p.val.
Example 1:
Input: root = [2,1,3], p = 1 Output: 2 Explanation: 1’s in-order successor node is 2. Note that both p and the return value is of TreeNode type.
Example 2:
Input: root = [5,3,6,2,4,null,null,1], p = 6
Output: null
Explanation: There is no in-order successor of the current node, so the answer is null.
Constraints:
- The number of nodes in the tree is in the range
[1, 10^4]. -10^5 <= Node.val <= 10^5- All Nodes will have unique values.
Approach: Binary Search on BST (Optimal Solution)
Algorithm
- Initialize
anstonull - While
rootis not null:- If
root.val > p.val,rootcould be the successor. Record it asansand move left (root = root.left) to look for a smaller candidate - Otherwise (
root.val <= p.val),rootcannot be the successor, so move right (root = root.right)
- If
- Return
ans
Key Insight
The in-order successor of p is the smallest value greater than p.val. As we walk down the tree, every node whose value exceeds p.val is a candidate; the best candidate is the smallest one, which is found by always trying to go left after recording a candidate. This is exactly a binary search for the lower bound of p.val + 1.
Time & Space Complexity
- Time Complexity: O(h) - follows a single root-to-leaf path, where h is the tree height
- Space Complexity: O(1) - only a couple of pointers are used
Java Implementation
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class InorderSuccessorInBST {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeNode ans = null;
while (root != null) {
if (root.val > p.val) {
ans = root;
root = root.left;
} else {
root = root.right;
}
}
return ans;
}
}
Example Walkthrough
For root = [2,1,3], p = 1:
- root=2, 2 > 1 → ans=2, move left to node 1
- root=1, 1 > 1 is false → move right (null)
- Return 2
For root = [5,3,6,2,4,null,null,1], p = 6:
- root=5, 5 > 6 false → move right to 6
- root=6, 6 > 6 false → move right (null)
- Return null — 6 is the maximum node in the tree
Alternative Approach: Exploiting Subtree Structure
The successor can also be described in two cases:
- If
phas a right subtree, the successor is the leftmost node of that right subtree (the smallest key greater thanp) - Otherwise, the successor is the lowest ancestor for which
plies in the left subtree
Both formulations lead to the same O(h) walk; the binary-search approach above is the simplest single-pass version.
Key Insights
- Smallest Greater Key: The successor is the lower bound — the minimum node strictly greater than
p.val - Candidate Tracking: Every node greater than
p.valis a candidate; always trying left finds the minimum - No Parent Pointers: The search works with just the root reference and
p - No Successor Case: Returns
nullnaturally whenpis the maximum node
The binary search approach is the optimal solution, providing O(h) time and O(1) space.