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
intermediate
Why is the inorder predecessor important in BST operations?
The inorder predecessor helps in operations like deletion of a node in BST, where replacing the node with its inorder predecessor maintains the BST property.
Click to reveal answer
beginner
In C++, what is a simple approach to find the inorder predecessor of a given node in a BST?
Check if the node has a left child. If yes, find the rightmost node in the left subtree. Otherwise, traverse up using parent pointers until you find a node which is the right child of its parent. Return that parent.
Click to reveal answer
What is the inorder predecessor of the smallest node in a BST?
AThere is no inorder predecessor
BThe node itself
CThe root node
DThe largest node in the BST
✗ Incorrect
The smallest node has no inorder predecessor because no node is smaller than it in the BST.
If a node has a left subtree, where is its inorder predecessor located?
AParent node
BLeftmost node in the right subtree
CRightmost node in the left subtree
DRoot 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?
AMove up until you find a node that is right child of its parent
BFind the leftmost node in the right subtree
CFind the rightmost node in the left subtree
DThere is no inorder predecessor
✗ Incorrect
You move up the tree until you find a node which is the right child of its parent; that parent is the inorder predecessor.
Why do we use the inorder predecessor in BST node deletion?
ATo find the smallest node
BTo maintain BST property after deletion
CTo find the root node
DTo balance the tree
✗ Incorrect
Replacing a deleted node with its inorder predecessor keeps the BST property intact.
Which traversal order is used to define the inorder predecessor?
APreorder traversal
BPostorder traversal
CLevel order traversal
DInorder traversal
✗ Incorrect
The inorder predecessor is defined based on the inorder traversal order.
Explain how to find the inorder predecessor of a node in a BST when the node has a left subtree.
Think about the largest value smaller than the node.
You got /3 concepts.
Describe the steps to find the inorder predecessor if the node has no left subtree.
Consider moving up until you find a node that is a right child.
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 smallest value larger than the given node
B. The node with the largest value smaller 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 in BST
Inorder traversal visits nodes in ascending order of their values.
Step 2: Define inorder predecessor
The inorder predecessor of a node is the node visited just before it in inorder traversal, which means it has the largest value smaller than the current node.
Final Answer:
The node with the largest value smaller than the given node -> Option B
Quick Check:
Inorder predecessor = largest smaller value [OK]
Hint: Predecessor is just before in sorted order [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++ function signatures correctly represents a function to find the inorder predecessor of a BST node given its pointer Node* root and Node* target?
easy
A. Node* findInorderPredecessor(Node* root, Node* target);
B. void findInorderPredecessor(Node* root, int target);
C. int findInorderPredecessor(Node* root, Node* target);
D. Node findInorderPredecessor(Node root, Node target);
Solution
Step 1: Analyze parameter types
The function needs the root pointer and the target node pointer to find the predecessor.
Step 2: Analyze return type
The predecessor is a node pointer, so return type should be Node*.
Final Answer:
Node* findInorderPredecessor(Node* root, Node* target); -> Option A
Quick Check:
Correct parameters and return type [OK]
Hint: Return pointer to predecessor node [OK]
Common Mistakes:
Using int return type instead of Node*
Passing target as int instead of Node*
Passing nodes by value instead of pointer
3. Consider the following BST and code snippet to find the inorder predecessor of node with value 15:
B. The function does not handle the case when target has no left subtree
C. The function returns nullptr always
D. The loop to find max in left subtree incorrectly goes left instead of right
Solution
Step 1: Check left subtree max search
To find inorder predecessor in left subtree, we must find the maximum node, which is the rightmost node, but code goes left repeatedly.
Step 2: Confirm other parts are correct
The rest of the code correctly handles cases without left subtree and initializes predecessor.
Final Answer:
The loop to find max in left subtree incorrectly goes left instead of right -> Option D
Quick Check:
Max in left subtree requires right traversal [OK]
Hint: Max in left subtree means go right, not left [OK]
Common Mistakes:
Going left instead of right to find max
Not checking if target->left is null
Confusing predecessor with successor
5. Given a BST with nodes inserted in this order: 25, 15, 50, 10, 22, 35, 70, 4, 12, 18, 24, 31, 44, 66, 90.
Write a function to find the inorder predecessor of a node with value 31. What will be the predecessor's value?
Hint: Use the BST property and consider both cases when the node has a left subtree or not.
hard
A. 25
B. 35
C. 24
D. 22
Solution
Step 1: Locate node 31
Node 31 is the left child of 35 and has no left subtree.
Step 2: Traverse from root to find predecessor
Start at root 25 (31 > 25, predecessor=25, go right to 50). 31 < 50, go left to 35. 31 < 35, go left to 31. Found target, predecessor=25.
Step 3: Verify with inorder traversal
Inorder traversal sequence around 31 is ... 24, 25, 31, 35 ... The predecessor of 31 is the largest smaller value, which is 24 (left subtree of 25).
Final Answer:
24 -> Option C
Quick Check:
Inorder predecessor of 31 is 24 [OK]
Hint: If no left child, predecessor is last smaller ancestor or max in left subtree [OK]
Common Mistakes:
Choosing 25 instead of 24
Choosing successor 35 instead of predecessor
Ignoring left subtree check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.