What is the inorder predecessor of a node in a Binary Search Tree (BST)?
The inorder predecessor of a node in a BST is the node that comes immediately before the given node in the inorder traversal of the BST. It is the largest node smaller than the given node.
Click to reveal answer
beginner
How do you find the inorder predecessor if the node has a left subtree?
If the node has a left subtree, the inorder predecessor is the rightmost (maximum) node in that left subtree.
Click to reveal answer
intermediate
What if the node does not have a left subtree? How to find its inorder predecessor?
If the node has no left subtree, move up the tree using parent pointers until you find a node which is the right child of its parent. That parent is the inorder predecessor.
Click to reveal answer
beginner
In C#, what data structure is commonly used to represent a BST node for finding inorder predecessor?
A BST node is commonly represented as a class with properties: int Value, Node Left, Node Right, and optionally Node Parent to help find inorder predecessor efficiently.
Click to reveal answer
intermediate
Why is the inorder predecessor important in BST operations?
The inorder predecessor is important for operations like deletion in BST, where replacing a node with its inorder predecessor maintains the BST property.
Click to reveal answer
What is the inorder predecessor of the smallest node in a BST?
AThe root node
BThe node itself
CThe largest node in the BST
DThere is no inorder predecessor
✗ Incorrect
The smallest node has no inorder predecessor because no node is smaller than it.
If a node has a left child, where do you look to find its inorder predecessor?
ALeft child's rightmost node
BRight child's rightmost node
CRight child's leftmost node
DLeft child's leftmost node
✗ Incorrect
The inorder predecessor is the rightmost (maximum) node in the left subtree.
If a node has no left subtree, how do you find its inorder predecessor?
AGo to the root node
BGo to the right child
CGo up until you find a node that is a right child of its parent
DThere is no inorder predecessor
✗ Incorrect
You move up the tree until you find a node that is the right child of its parent; that parent is the inorder predecessor.
Which of these is NOT a typical property of a BST node class in C#?
ALeft
BHeight
CRight
DValue
✗ Incorrect
Height is not a typical property for a basic BST node; it is used in AVL trees.
Why do we use the inorder predecessor in BST deletion?
ATo replace the deleted node and maintain BST order
BTo find the smallest node
CTo find the root node
DTo balance the tree
✗ Incorrect
Replacing the deleted node with its inorder predecessor keeps the BST property intact.
Explain how to find the inorder predecessor of a node in a BST with and without a left subtree.
Think about the inorder traversal order and how predecessor relates to it.
You got /2 concepts.
Describe why the inorder predecessor is useful when deleting a node from a BST.
Consider how BST nodes are ordered and how deletion affects structure.
You got /3 concepts.
Practice
(1/5)
1. What is the inorder predecessor of a node in a Binary Search Tree (BST)?
easy
A. The node with the largest value smaller than the given node
B. The node with the smallest value larger than the given node
C. The left child of the given node
D. The right child of the given node
Solution
Step 1: Understand inorder traversal order
Inorder traversal visits nodes in ascending order for BST.
Step 2: Define predecessor in inorder traversal
The predecessor is the node visited just before the current node, so it must have the largest value smaller than the current node.
Final Answer:
The node with the largest value smaller than the given node -> Option A
Quick Check:
Inorder predecessor = largest smaller value [OK]
Hint: Predecessor is largest smaller value in BST [OK]
Common Mistakes:
Confusing predecessor with successor
Thinking predecessor is always left child
Assuming predecessor is the parent node
2. Which of the following C# code snippets correctly finds the inorder predecessor of a node with a left subtree in a BST?
easy
A. Node predecessor = node.Left; while (predecessor.Left != null) predecessor = predecessor.Left;
B. Node predecessor = node.Right; while (predecessor.Left != null) predecessor = predecessor.Left;
C. Node predecessor = node.Right; while (predecessor.Right != null) predecessor = predecessor.Right;
D. Node predecessor = node.Left; while (predecessor.Right != null) predecessor = predecessor.Right;
Solution
Step 1: Check where predecessor lies if left subtree exists
Predecessor is the rightmost node in the left subtree.
Step 2: Analyze code snippets
Node predecessor = node.Left; while (predecessor.Right != null) predecessor = predecessor.Right; starts at left child and moves right until no more right child, correctly finding predecessor.
Final Answer:
Node predecessor = node.Left; while (predecessor.Right != null) predecessor = predecessor.Right; -> Option D
Quick Check:
Left subtree's rightmost node = predecessor [OK]
Hint: Go left once, then right as far as possible [OK]
Common Mistakes:
Going left repeatedly instead of right in left subtree
Starting from right child instead of left
Ignoring the while loop to find rightmost node
3. Given the BST below and the node with value 15, what is the inorder predecessor?
20
/ \
10 30
\
15
medium
A. 30
B. 10
C. 20
D. 15
Solution
Step 1: Locate node 15 and check left subtree
Node 15 has no left child, so predecessor is ancestor smaller than 15.
Step 2: Find nearest ancestor smaller than 15
Ancestor 10 is smaller than 15 and is the closest on the path.
Final Answer:
10 -> Option B
Quick Check:
Predecessor of 15 is 10 [OK]
Hint: If no left child, go up to smaller ancestor [OK]
Common Mistakes:
Choosing 20 which is larger than 15
Choosing 15 itself
Choosing 30 which is right subtree
4. Consider the following C# method to find the inorder predecessor of a node in a BST. What is the bug?
Node FindPredecessor(Node root, Node node) {
if (node.Left != null) {
Node temp = node.Left;
while (temp.Left != null) {
temp = temp.Left;
}
return temp;
}
Node predecessor = null;
Node current = root;
while (current != null) {
if (node.Value > current.Value) {
predecessor = current;
current = current.Right;
} else {
current = current.Left;
}
}
return predecessor;
}
medium
A. The while loop should check temp.Right, not temp.Left
B. The method does not handle null root
C. The method returns node itself as predecessor
D. The method incorrectly updates predecessor in the else block
Solution
Step 1: Analyze left subtree traversal
Predecessor in left subtree is rightmost node, so loop should move right, not left.
Step 2: Identify incorrect loop condition
Current code loops while temp.Left != null, should be temp.Right != null to find rightmost node.
Final Answer:
The while loop should check temp.Right, not temp.Left -> Option A
Quick Check:
Rightmost node in left subtree = predecessor [OK]
Hint: Go right in left subtree to find predecessor [OK]
Common Mistakes:
Checking left child repeatedly instead of right
Ignoring null root case (not critical here)
Confusing predecessor update logic
5. You want to find the inorder predecessor of a node with value 25 in the BST below:
40
/ \
20 60
/ \ / \
10 30 50 70
/
25
Which node is the inorder predecessor of 25?
hard
A. 30
B. 10
C. 20
D. 25 has no predecessor
Solution
Step 1: Check if node 25 has left subtree
Node 25 has no left child, so predecessor is ancestor smaller than 25.
Step 2: Find nearest ancestor smaller than 25
Ancestor 20 is smaller than 25 and closest on path from root to 25.
Final Answer:
20 -> Option C
Quick Check:
Predecessor of 25 is 20 [OK]
Hint: If no left child, predecessor is nearest smaller ancestor [OK]
Common Mistakes:
Choosing 30 which is larger than 25
Choosing 10 which is smaller but not nearest ancestor
Thinking 25 has no predecessor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.