Discover how to find the 'just before' value in a tree without checking every node!
Why BST Inorder Predecessor in DSA Typescript?
Imagine you have a family tree drawn on paper, and you want to find the person who comes just before someone in the family order. Doing this by looking at every name and comparing takes a lot of time and can get confusing.
Manually searching for the person just before another means checking many names one by one. This is slow and easy to make mistakes, especially if the tree is big or not sorted well.
The BST Inorder Predecessor helps find the immediate previous value in a sorted tree quickly by following a clear path, avoiding unnecessary checks and mistakes.
function findPredecessor(root, target) {
let values = [];
inorderTraversal(root, values);
for (let i = 1; i < values.length; i++) {
if (values[i] === target) return values[i - 1];
}
return null;
}function findInorderPredecessor(root, target) {
let predecessor = null;
let current = root;
while (current) {
if (target > current.value) {
predecessor = current;
current = current.right;
} else {
current = current.left;
}
}
return predecessor ? predecessor.value : null;
}This lets you quickly find the closest smaller value in a sorted tree, making searches and updates much faster and more reliable.
In a contact list sorted by names, finding the contact that comes just before a given name helps in quick navigation and suggestions.
Manual search is slow and error-prone for finding previous values.
BST Inorder Predecessor uses tree properties to find the answer efficiently.
This improves speed and accuracy in sorted data operations.
Practice
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 BQuick Check:
Inorder predecessor = largest smaller value [OK]
- Confusing predecessor with successor
- Thinking predecessor is always left child
- Assuming predecessor is the parent node
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 CQuick Check:
Correct inputs and output type [OK]
- Missing root parameter
- Returning wrong type like number or boolean
- No parameters or void return type
20
/ \
10 30
\
15
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 AQuick Check:
Predecessor of 15 = 10 [OK]
- Choosing the node itself as predecessor
- Choosing parent node incorrectly
- Confusing predecessor with successor
function inorderPredecessor(root: TreeNode | null, node: TreeNode): TreeNode | null {
let predecessor: TreeNode | null = null;
while (root) {
if (node.val <= root.val) {
root = root.left;
} else {
predecessor = root;
root = root.right;
}
}
return predecessor;
}
What is the bug in this code?Solution
Step 1: Understand inorder predecessor logic
If node has a left subtree, predecessor is the rightmost node in that subtree, which this code does not check.Step 2: Analyze code behavior
The code only moves down from root to find predecessor by comparing values, missing the left subtree check for node.Final Answer:
It does not handle the case when node has a left subtree -> Option AQuick Check:
Left subtree check missing for predecessor [OK]
- Ignoring left subtree of node
- Assuming predecessor is always ancestor
- Confusing predecessor with successor logic
40
/ \
20 60
/ \ / \
10 30 50 70
/
25
Which steps correctly find the inorder predecessor of 25?Solution
Step 1: Check left subtree of node 25
Node 25 has no left child, so no predecessor in left subtree.Step 2: Move up to ancestors to find predecessor
Ancestor nodes smaller than 25 are candidates; 20 is the closest smaller ancestor.Final Answer:
Check left subtree of 25 (none), then move up to ancestors; predecessor is 20 -> Option DQuick Check:
Predecessor found by ancestor smaller than node [OK]
- Choosing successor instead of predecessor
- Ignoring ancestors when left subtree is empty
- Picking wrong ancestor value
Ready to prove it?
Pass the test and this pattern moves unknown
