BST Inorder Successor in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to find the inorder successor in a Binary Search Tree changes as the tree grows.
How does the number of steps grow when the tree has more nodes?
Analyze the time complexity of the following code snippet.
function inorderSuccessor(root, p) {
if (p.right) {
let curr = p.right;
while (curr.left) curr = curr.left;
return curr;
}
let successor = null;
let curr = root;
while (curr) {
if (p.val < curr.val) {
successor = curr;
curr = curr.left;
} else {
curr = curr.right;
}
}
return successor;
}
This code finds the next bigger node (inorder successor) of a given node in a BST.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing down the tree nodes using while loops.
- How many times: At most, the number of steps equals the height of the tree.
As the tree grows, the height usually grows too, so the steps increase roughly with the height.
| Input Size (n) | Approx. Operations (height) |
|---|---|
| 10 | About 3 to 4 steps |
| 100 | About 6 to 7 steps |
| 1000 | About 9 to 10 steps |
Pattern observation: The number of steps grows slowly, roughly proportional to the tree height, which is often much smaller than total nodes.
Time Complexity: O(h)
This means the time depends on the height of the tree, not the total number of nodes.
[X] Wrong: "Finding the inorder successor always takes time proportional to the total number of nodes."
[OK] Correct: The search only follows a path down the tree, so it depends on the height, not all nodes.
Understanding this helps you explain how tree shape affects search speed, a key skill for working with trees in real problems.
"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 = next bigger key [OK]
- Confusing successor with predecessor
- Thinking successor is always the right child
- Assuming successor is the root node
Solution
Step 1: Identify required inputs
To find the inorder successor, we need the BST root to traverse and the target node to find its successor.Step 2: Match function signature
function inorderSuccessor(root, node) {} correctly takes both root and node as parameters, enabling traversal and comparison.Final Answer:
function inorderSuccessor(root, node) {} -> Option BQuick Check:
Function needs root and node [OK]
- Using only node without root
- Wrong parameter order
- Incorrect function naming
20
/ \
10 30
15
Solution
Step 1: Locate node 15 and its right subtree
Node 15 has no right child, so successor is an ancestor.Step 2: Find the lowest ancestor whose left child is also ancestor of 15
Node 20 is the first ancestor where 15 is in left subtree, so successor is 20.Final Answer:
20 -> Option CQuick Check:
Successor of 15 is 20 [OK]
- Choosing right child when none exists
- Picking the node itself as successor
- Confusing successor with predecessor
function inorderSuccessor(root, node) {
if (!root) return null;
if (node.val >= root.val) {
return inorderSuccessor(root.right, node);
} else {
let left = inorderSuccessor(root.left, node);
return left ? left : root;
}
}
What is the main issue with this code?Solution
Step 1: Analyze code logic
The code only traverses the tree comparing values but never checks if node has a right subtree.Step 2: Identify missing right subtree check
If node has a right child, successor is the leftmost node in right subtree, which this code ignores.Final Answer:
It does not handle the case when node has a right subtree -> Option AQuick Check:
Missing right subtree check causes wrong successor [OK]
- Ignoring right subtree
- Wrong base case
- Returning node instead of successor
20
/ \
10 30
/ \
25 40
What is the inorder successor of node 25 and why?Solution
Step 1: Check if node 25 has right subtree
Node 25 has no right child, so successor is an ancestor.Step 2: Find the lowest ancestor whose left subtree contains node 25
Node 30 is the parent of 25 and is the next bigger node, so successor is 30.Final Answer:
30, because 25 has no right child and 30 is the next bigger ancestor -> Option DQuick Check:
Successor of 25 is 30 [OK]
- Choosing right child of parent instead of parent
- Assuming no successor if no right child
- Confusing root as successor
Ready to prove it?
Pass the test and this pattern moves unknown
