What is the inorder successor of a node in a Binary Search Tree (BST)?
The inorder successor of a node in a BST is the node with the smallest key greater than the given node's key. It is the next node visited in an inorder traversal.
Click to reveal answer
beginner
How do you find the inorder successor if the node has a right child?
If the node has a right child, the inorder successor is the leftmost node in the node's right subtree.
Click to reveal answer
intermediate
How do you find the inorder successor if the node has no right child?
If the node has no right child, the inorder successor is one of its ancestors. Specifically, it is the lowest ancestor of the node whose left child is also an ancestor of the node.
Click to reveal answer
intermediate
What is the time complexity of finding the inorder successor in a BST?
The time complexity is O(h), where h is the height of the BST, because you may need to traverse up or down the tree along a path.
Click to reveal answer
intermediate
Explain why the inorder successor is important in BST operations.
The inorder successor is important for operations like deletion in BSTs, where replacing a node with its inorder successor maintains the BST property. It also helps in traversing the BST in sorted order.
Click to reveal answer
What is the inorder successor of the maximum node in a BST?
AThe root node
BNull (no successor)
CThe minimum node
DThe parent node
✗ Incorrect
The maximum node has no inorder successor because there is no node with a greater key.
If a node has a right child, where do you look to find its inorder successor?
ARight child's right subtree
BParent node
CRight child's leftmost node
DLeft child's rightmost node
✗ Incorrect
The inorder successor is the leftmost node in the right subtree.
If a node has no right child, how do you find its inorder successor?
AGo up to the lowest ancestor whose left child is also an ancestor of the node
BGo to the root node
CGo to the leftmost node in the tree
DGo to the rightmost node in the left subtree
✗ Incorrect
You move up the tree to find the lowest ancestor where the node is in the left subtree.
What is the worst-case time complexity to find the inorder successor in a BST?
AO(h)
BO(log n)
CO(n)
DO(1)
✗ Incorrect
The time depends on the height h of the BST, which can be up to O(n) in worst case but generally O(log n).
Why is the inorder successor used in BST node deletion?
ATo find the smallest node in the tree
BTo balance the tree
CTo find the parent of the node
DTo replace the deleted node and maintain BST order
✗ Incorrect
Replacing the deleted node with its inorder successor keeps the BST property intact.
Describe the steps to find the inorder successor of a given node in a BST.
Think about right subtree first, then ancestors.
You got /3 concepts.
Explain why the inorder successor is important when deleting a node from a BST.
Consider how BST order is preserved after deletion.
You got /3 concepts.
Practice
(1/5)
1. What is the inorder successor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest key greater than the given node's key
B. The node with the largest key smaller than the given node's key
C. The root node of the BST
D. The left child of the given node
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 A
Quick Check:
Inorder successor = smallest greater key [OK]
Hint: Inorder successor is next bigger node in sorted order [OK]
Common Mistakes:
Confusing successor with predecessor
Thinking successor is always right child
Assuming successor is parent node
2. Which of the following C# method signatures correctly represents a function to find the inorder successor of a BST node?
easy
A. public void InorderSuccessor(TreeNode root, TreeNode p)
B. public TreeNode InorderSuccessor(TreeNode root, TreeNode p)
C. public TreeNode FindSuccessor(TreeNode p)
D. public int InorderSuccessor(int root, int p)
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 B
Quick Check:
Correct signature uses TreeNode parameters and returns TreeNode [OK]
Hint: Successor method needs root and target node, returns TreeNode [OK]
Common Mistakes:
Using int instead of TreeNode for parameters
Returning void instead of TreeNode
Missing root parameter
3. Given the BST below and the node with value 15, what is the inorder successor?
20
/ \
10 30
\ \
15 40
TreeNode successor = InorderSuccessor(root, node15); Console.WriteLine(successor.val);
medium
A. 40
B. 30
C. 20
D. 15
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 C
Quick Check:
Successor of 15 is 20 [OK]
Hint: If no right child, go up to first bigger ancestor [OK]
Common Mistakes:
Choosing right child 30 incorrectly
Choosing node itself as successor
Choosing 40 which is too far
4. The following code attempts to find the inorder successor of a node in a BST but returns null incorrectly. What is the bug?
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;
}
medium
A. The code does not handle the case when p has no right child
B. The while loop condition should be curr.right instead of curr.left
C. The comparison p.val < root.val should be p.val <= root.val
D. The code is correct and should not return null incorrectly
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 D
Quick Check:
Code handles both cases correctly [OK]
Hint: Check both right subtree and ancestor paths for successor [OK]
Common Mistakes:
Changing left to right in while loop incorrectly
Using <= instead of < causing wrong ancestor selection
Assuming code misses no-right-child case
5. You have a BST and want to find the inorder successor of the node with value 50. The BST is:
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?
hard
A. null, because the node is not in the BST
B. The smallest node greater than 50, which is 60
C. The largest node smaller than 50, which is 40
D. The root node, which is 40
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 A
Quick Check:
Node missing means no successor found [OK]
Hint: Successor only valid if node exists in BST [OK]
Common Mistakes:
Returning closest bigger node even if node missing
Returning root node incorrectly
Returning predecessor instead of null
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.