What is the inorder predecessor in a Binary Search Tree (BST)?
The inorder predecessor of a node in a BST is the node that comes immediately before it in the inorder traversal. 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 has no 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 in BST, where you replace a node with its inorder predecessor to maintain BST properties.
Click to reveal answer
beginner
In TypeScript, what is a simple approach to find the inorder predecessor of a node in a BST?
Traverse to the left child, then keep moving right until no more right child exists. If no left child, use parent pointers to find the predecessor. This can be done with a loop and conditional checks.
Click to reveal answer
What is the inorder predecessor of a node with a left subtree in a BST?
AThe leftmost node in the right subtree
BThe rightmost node in the left subtree
CThe root node
DThe parent node always
✗ 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 to the parent until the node is a right child
BGo to the right child
CGo to the left child
DThere is no predecessor
✗ Incorrect
You move up the tree until you find a node that is a right child of its parent; that parent is the predecessor.
Why is the inorder predecessor useful in BST deletion?
AIt balances the tree
BIt helps find the smallest node
CIt deletes the root node
DIt helps replace a node to maintain BST order
✗ Incorrect
Replacing a node with its inorder predecessor maintains the BST property during deletion.
In inorder traversal, the predecessor of a node is:
AThe previous node visited
BThe next node visited
CThe root node
DThe leaf node
✗ Incorrect
The inorder predecessor is the node visited immediately before the current node in inorder traversal.
Which of these is NOT true about inorder predecessor?
AIt is always smaller than the node
BIt can be found in the left subtree
CIt is always the parent node
DIt is the largest node smaller than the given node
✗ Incorrect
The inorder predecessor is not always the parent; it depends on the tree structure.
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 /3 concepts.
Describe why the inorder predecessor is important when deleting a node from a BST.
Consider how BST properties must stay true after deletion.
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 right child of 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 node with the smallest value larger than 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 given 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 TypeScript function signature correctly represents a method to find the inorder predecessor of a BST node?
easy
A. function findInorderPredecessor(root: TreeNode): void
B. function findInorderPredecessor(node: TreeNode): number
C. function findInorderPredecessor(root: TreeNode, node: TreeNode): TreeNode | null
D. function findInorderPredecessor(): boolean
Solution
Step 1: Identify required inputs and outputs
To find the inorder predecessor, we need the BST root and the target node, and we return the predecessor node or null if none.
Step 2: Match function signature
function findInorderPredecessor(root: TreeNode, node: TreeNode): TreeNode | null matches this: it takes root and node, returns TreeNode or null.
Final Answer:
function findInorderPredecessor(root: TreeNode, node: TreeNode): TreeNode | null -> Option C
Quick Check:
Correct inputs and output type [OK]
Hint: Function needs root, node, returns node or null [OK]
Common Mistakes:
Missing root parameter
Returning wrong type like number or boolean
No parameters or void return type
3. Given the BST below and the node with value 15, what is the inorder predecessor?
20
/ \
10 30
\
15
medium
A. 10
B. 15
C. 20
D. 30
Solution
Step 1: Perform inorder traversal around node 15
Inorder traversal visits nodes in order: 10, 15, 20, 30.
Step 2: Find node before 15 in traversal
The node before 15 is 10, so 10 is the inorder predecessor.
Final Answer:
10 -> Option A
Quick Check:
Predecessor of 15 = 10 [OK]
Hint: Predecessor is previous in sorted order [OK]
Common Mistakes:
Choosing the node itself as predecessor
Choosing parent node incorrectly
Confusing predecessor with successor
4. Consider this TypeScript code snippet to find the inorder predecessor of a node in a BST: