📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the inorder predecessor of a given node. The inorder predecessor is the node with the largest value smaller than the given node's value.Think of the BST as a family tree sorted by age, and you want to find the closest older sibling who is younger than a specific person.
🎯 Goal: Build a TypeScript program that creates a BST, sets a target value, finds the inorder predecessor of that value in the BST, and prints the predecessor's value or null if none exists.
📋 What You'll Learn
Create a BST with the exact nodes: 20, 10, 30, 5, 15, 25, 35
Create a variable targetValue set to 15
Write a function findInorderPredecessor that finds the inorder predecessor of targetValue in the BST
Print the predecessor's value or null if no predecessor exists
💡 Why This Matters
🌍 Real World
Finding inorder predecessors is useful in database indexing and navigation systems where you need to find the closest smaller value to a given key.
💼 Career
Understanding BST traversal and predecessor/successor logic is important for software engineers working on search algorithms, data indexing, and optimization problems.
Progress0 / 4 steps
1
Create the BST nodes
Create a class called TreeNode with val, left, and right properties. Then create the BST with nodes having values 20, 10, 30, 5, 15, 25, and 35 arranged correctly as a BST.
DSA Typescript
Hint
Define a TreeNode class with val, left, and right. Then create nodes and link them to form the BST.
2
Set the target value
Create a variable called targetValue and set it to 15.
DSA Typescript
Hint
Just create a constant named targetValue and assign it the number 15.
3
Write the function to find the inorder predecessor
Write a function called findInorderPredecessor that takes root of type TreeNode | null and target of type number. It should return the inorder predecessor node or null if none exists. Use BST properties to find the predecessor efficiently.
DSA Typescript
Hint
Use a loop to traverse the BST. Move left if target is less or equal to current node's value. Otherwise, update predecessor and move right.
4
Print the inorder predecessor value
Call findInorderPredecessor with root and targetValue. Print the predecessor's val if it exists, otherwise print null.
DSA Typescript
Hint
Call the function and print the predecessor's value if found, else print null.
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: