BST Inorder Successor in DSA Java - Time & Space Complexity
We want to understand how long it takes to find the next bigger value in a Binary Search Tree (BST) after a given node.
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
if (p.right != null) {
TreeNode curr = p.right;
while (curr.left != null) {
curr = curr.left;
}
return curr;
}
TreeNode successor = null;
TreeNode curr = root;
while (curr != null) {
if (p.val < curr.val) {
successor = curr;
curr = curr.left;
} else {
curr = curr.right;
}
}
return successor;
}
This code finds the inorder successor of a node in a BST, either by going right then left or by searching from the root.
- Primary operation: Traversing down the tree nodes using while loops.
- How many times: At most, one path from a node down to a leaf, which is up to the tree height.
As the tree grows taller, the number of steps to find the successor grows roughly with the height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4-5 steps (height of small tree) |
| 100 | Up to 7-8 steps (taller tree) |
| 1000 | Up to 10 steps (even taller tree) |
Pattern observation: The steps grow slowly with input size, depending on tree height, not total nodes.
Time Complexity: O(h)
This means the time depends on the height of the tree, which is the longest path from root to leaf.
[X] Wrong: "Finding the inorder successor always takes time proportional to the number of nodes (n)."
[OK] Correct: The search only follows one path down the tree, so it depends on the height, not all nodes.
Understanding how tree height affects search time helps you explain and optimize tree operations clearly in interviews.
"What if the BST is perfectly balanced versus completely skewed? How would the time complexity change?"
Practice
Solution
Step 1: Understand inorder traversal order
Inorder traversal visits nodes in ascending order of their keys in a BST.Step 2: Define inorder successor
The inorder successor of a node is the next node visited after it in inorder traversal, which is the smallest node greater than it.Final Answer:
The node with the smallest key greater than the given node's key -> Option AQuick Check:
Inorder successor = smallest greater key [OK]
- Confusing successor with predecessor
- Thinking successor is always right child
- Assuming successor is parent node
Solution
Step 1: Identify required inputs and outputs
To find inorder successor, we need the BST root and the target node; output is the successor node.Step 2: Match method signature
public Node inorderSuccessor(Node root, Node p) takes root and node, returns Node, matching requirements.Final Answer:
public Node inorderSuccessor(Node root, Node p) -> Option BQuick Check:
Input root and node, output node [OK]
- Missing root parameter
- Returning int instead of Node
- Using void return type
20
/ \
10 30
\
15
Solution
Step 1: Locate node 15 and check right subtree
Node 15 has no right child, so successor is ancestor with greater value.Step 2: Find lowest ancestor greater than 15
Ancestor 20 is the next bigger node after 15.Final Answer:
20 -> Option AQuick Check:
Next bigger after 15 is 20 [OK]
- Choosing 30 which is too big
- Choosing 10 which is smaller
- Choosing the node itself
public Node inorderSuccessor(Node root, Node p) {
if (p.right != null) {
Node curr = p.right;
while (curr.left != null) {
curr = curr.left;
}
return curr;
}
Node succ = null;
while (root != null) {
if (p.val < root.val) {
succ = root;
root = root.left;
} else {
root = root.right;
}
}
return succ;
}
What is the main bug in this code?Solution
Step 1: Analyze right subtree handling
If p has right child, code correctly finds leftmost node in right subtree.Step 2: Analyze ancestor search loop
Loop updates successor when p.val < root.val, else moves right. This is correct logic.Step 3: Identify potential null return
If p is the largest node, no successor exists, so null return is valid. Otherwise, code works.Final Answer:
It may return null even if successor exists -> Option DQuick Check:
Null return possible if logic misses successor [OK]
- Assuming null means no successor always
- Ignoring right subtree case
- Misreading comparison operators
40
/ \
20 60
/ \ / \
10 30 50 70
\
25
Which node is the inorder successor of 25 and why?Solution
Step 1: Check if node 25 has right child
Node 25 has no right child, so successor is ancestor or next bigger node.Step 2: Find nearest ancestor greater than 25
Node 40 is the nearest ancestor greater than 25, so it is the inorder successor.Final Answer:
40, because 25 has no right child and 40 is the nearest bigger ancestor -> Option CQuick Check:
Next bigger node after 25 is 40 [OK]
- Choosing 30 which is smaller than 25
- Choosing 50 which is too far
- Choosing parent 20 which is smaller
Ready to prove it?
Pass the test and this pattern moves unknown
