BST Inorder Successor in DSA C# - Time & Space Complexity
We want to know how long it takes to find the next bigger node in a Binary Search Tree (BST).
How does the time grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
public TreeNode InorderSuccessor(TreeNode root, TreeNode p) {
TreeNode successor = null;
while (root != null) {
if (p.val < root.val) {
successor = root;
root = root.left;
} else {
root = root.right;
}
}
return successor;
}
This code finds the inorder successor of a given node in a BST by walking down the tree.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves down the tree nodes.
- How many times: At most once per level of the tree, from root to leaf.
As the tree grows taller, the number of steps to find the successor grows roughly with the height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (small tree) | About 3 to 4 steps |
| 100 (medium tree) | About 6 to 7 steps |
| 1000 (large tree) | About 9 to 10 steps |
Pattern observation: The steps grow slowly as the tree height grows, not directly with total nodes.
Time Complexity: O(h)
This means the time depends on the tree height, which is the number of levels from root to leaf.
[X] Wrong: "Finding the successor always takes time proportional to the total number of nodes (n)."
[OK] Correct: The search only moves down one path, so it depends on the height, not all nodes.
Understanding how tree height affects search time helps you explain and optimize tree operations clearly.
"What if the BST is balanced versus completely unbalanced? 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 key greater than the node's key.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: Check parameter types for BST nodes
The method should accept the BST root and the target node to find successor, both as TreeNode objects.Step 2: Check return type
The method returns the successor node, so return type must be TreeNode, not int or void.Final Answer:
public TreeNode InorderSuccessor(TreeNode root, TreeNode p) -> Option BQuick Check:
Correct signature uses TreeNode parameters and returns TreeNode [OK]
- Using int instead of TreeNode for parameters
- Returning void instead of TreeNode
- Missing root parameter
20
/ \
10 30
\ \
15 40
TreeNode successor = InorderSuccessor(root, node15); Console.WriteLine(successor.val);Solution
Step 1: Locate node 15 and find its inorder successor
Node 15 has no right child, so successor is the lowest ancestor whose left child is also ancestor of 15, which is 20.Step 2: Confirm successor value
20 is the next bigger node after 15 in inorder traversal.Final Answer:
20 -> Option CQuick Check:
Successor of 15 is 20 [OK]
- Choosing right child 30 incorrectly
- Choosing node itself as successor
- Choosing 40 which is too far
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;
while (root != null) {
if (p.val < root.val) {
successor = root;
root = root.left;
} else {
root = root.right;
}
}
return successor;
}
Solution
Step 1: Analyze right subtree handling
If p has right child, code correctly finds leftmost node in right subtree as successor.Step 2: Analyze ancestor traversal
If no right child, code moves up tree to find first ancestor greater than p, correctly updating successor.Step 3: Confirm no logical errors
Comparisons and loops are correct; code returns null only if no successor exists (p is max node).Final Answer:
The code is correct and should not return null incorrectly -> Option DQuick Check:
Code handles both cases correctly [OK]
- Changing left to right in while loop incorrectly
- Using <= instead of < causing wrong ancestor selection
- Assuming code misses no-right-child case
40
/ \
20 60
/ \ \
10 30 70
\
80
If the node with value 50 does not exist in the tree, what should your inorder successor function return?Solution
Step 1: Confirm node presence in BST
Node with value 50 is not present in the BST, so no direct successor can be found.Step 2: Understand successor function behavior
Successor function requires the node to find its successor; if node not found, function should return null or indicate absence.Final Answer:
null, because the node is not in the BST -> Option AQuick Check:
Node missing means no successor found [OK]
- Returning closest bigger node even if node missing
- Returning root node incorrectly
- Returning predecessor instead of null
Ready to prove it?
Pass the test and this pattern moves unknown
