medium Inorder Successor in BST
Problem Statement
Given a root node of the binary search tree and node p, return the value of the in-order successor of node p in the given tree. If the given node has no in-order successor, return -1.
The in-order successor of a node is the next node in the in-order traversal of the BST, which means it is the node with the smallest value greater than the given node.
Examples
Example 1:
- Input: root = [3,2,4,1], p = 2
- Expected Output: 3
- Justification: In the in-order traversal [1,2,3,4], the next node after 2 is 3, making it the in-order successor.
Example 2:
- Input: root = [8,3,10,1,6,null,14,null,null,4,7,13], p = 6
- Expected Output: 10
- Justification: The in-order traversal of the tree is [1,3,4,6,7,8,10,13,14]. The smallest value greater than 6 is 7, making it the in-order successor.
Example 3:
- Input: root = [15,10,20,8,12,16,25], p = 25
- Expected Output: -1
- Justification: In the in-order traversal [8,10,12,15,16,20,25], there is no node after 25. Hence, the expected output is -1.
Try it yourself
Try solving this question here:
✅ Solution Inorder Successor in BST
Problem Statement
Given a root node of the binary search tree and node p, return the value of the in-order successor of node p in the given tree. If the given node has no in-order successor, return -1.
The in-order successor of a node is the next node in the in-order traversal of the BST, which means it is the node with the smallest value greater than the given node.
Examples
Example 1:
- Input: root = [3,2,4,1], p = 2
- Expected Output: 3
- Justification: In the in-order traversal [1,2,3,4], the next node after 2 is 3, making it the in-order successor.
Example 2:
- Input: root = [8,3,10,1,6,null,14,null,null,4,7,13], p = 6
- Expected Output: 10
- Justification: The in-order traversal of the tree is [1,3,4,6,7,8,10,13,14]. The smallest value greater than 6 is 7, making it the in-order successor.
Example 3:
- Input: root = [15,10,20,8,12,16,25], p = 25
- Expected Output: -1
- Justification: In the in-order traversal [8,10,12,15,16,20,25], there is no node after 25. Hence, the expected output is -1.
Solution
To address the problem of finding the in-order successor in a Binary Search Tree (BST), our strategy capitalizes on the BST's ordered nature. The in-order successor of a node is the node that appears immediately after the given node when the tree is traversed in an in-order manner (left, root, right).
For any node p, if it has a right subtree, its in-order successor is the left-most node in its right subtree. Conversely, if the node has no right subtree, the successor is one of its ancestors, specifically the one that p would be the left child of in the path from root to p. This insight leads to a two-fold approach: first, descend towards the given node p, tracking the last node encountered that is greater than p as a potential successor; second, if p has a right child, override the tracked ancestor with the left-most child of p's right subtree. This method is efficient because it traverses each relevant part of the tree at most once, directly homing in on the successor by leveraging the BST property that left children are less than their parents and right children are greater.
Step-by-Step Algorithm
- Start from the root of the BST.
- Initialize a variable to store the potential successor as
-1or an equivalent value indicating no successor found. - While the current node is not null:
- If the value of the current node is less than or equal to the p, move to the right child.
- If the value of the current node is greater than the p, update the potential successor to the current node and move to the left child.
- Continue this process until the entire tree is traversed or the exact match is found.
- Return the value of the potential successor if found; otherwise, return -1 indicating no successor.
Algorithm Walkthrough
Let's consider the input: root = [8,3,10,1,6,null,14,null,null,4,7,13], p = 6
Let's walk through the algorithm to find the in-order successor of p = 6 using the same tree structure as before:
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
-
Start at the root of the BST, which is
8. Since6is less than8, we move to the left child of8, which is3, to find6.successor= 8 -
Move to the right child of
3, as3is not greater than6. -
Move to the right child of
6, as6is not greater than6. -
Inspect the right subtree of
6, which leads us to7. Since7does not have a left child, it is the left-most node in the right subtree of6and thus the in-order successor of6. -
End of Algorithm: The in-order successor of the node with value
6in the given BST is7.
Code
// class TreeNode {
// int val;
// TreeNode left, right;
// TreeNode(int x) { val = x; }
// }
public class Solution {
public int inorderSuccessor(TreeNode root, int p) {
TreeNode successor = null;
while (root != null) {
// If root value is greater than p, root could be a successor.
if (p < root.val) {
successor = root;
root = root.left;
} else {
root = root.right;
}
}
// Return the value of the successor or -1 if it does not exist.
return successor != null ? successor.val : -1;
}
public static void main(String[] args) {
// Example of constructing a tree and finding an inorder successor
TreeNode root = new TreeNode(8);
root.left = new TreeNode(3);
root.right = new TreeNode(10);
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(6);
root.right.right = new TreeNode(14);
root.left.right.left = new TreeNode(4);
root.left.right.right = new TreeNode(7);
root.right.right.left = new TreeNode(13);
int p = 6; // The value to find the inorder successor of
Solution solution = new Solution();
System.out.println(
"Inorder Successor value: " + solution.inorderSuccessor(root, p)
);
}
}
Complexity Analysis
- Time Complexity: The algorithm has a time complexity of
, where h is the height of the tree. This is because we are traversing the tree from root to leaf, and in the worst case, we might have to traverse the height of the tree. - Space Complexity: The space complexity is
since we are only using a constant amount of extra space for the variables, regardless of the size of the input tree.
🤖 Don't fully get this? Learn it with Claude
Stuck on Inorder Successor in BST? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Inorder Successor in BST** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Inorder Successor in BST** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Inorder Successor in BST**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Inorder Successor in BST**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.