📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the next bigger number after a given number in the tree. This is called the inorder successor.Imagine the BST as a family tree of numbers, where each number has smaller numbers on the left and bigger numbers on the right. Finding the inorder successor is like finding the next person in line after someone in a sorted list.
🎯 Goal: Build a program that finds the inorder successor of a given node value in a BST. You will create the BST nodes, set up the tree, write the logic to find the successor, and print the result.
📋 What You'll Learn
Create BST nodes with exact values and structure
Set a target node value to find its inorder successor
Implement the inorder successor logic using BST properties
Print the inorder successor value or 'null' if none exists
💡 Why This Matters
🌍 Real World
Finding the inorder successor is useful in database indexing, scheduling tasks in order, and navigating sorted data efficiently.
💼 Career
Understanding BST operations like inorder successor is important for software engineers working on search algorithms, data retrieval, and optimization problems.
Progress0 / 4 steps
1
Create BST nodes and build the tree
Create a class called Node with a constructor that takes value, left, and right parameters. Then create nodes with these exact values and structure: root = new Node(20), root.left = new Node(10), root.right = new Node(30), root.left.left = new Node(5), root.left.right = new Node(15).
DSA Javascript
Hint
Remember to create the Node class first, then build the tree by linking nodes with left and right.
2
Set the target node value to find successor
Create a variable called targetValue and set it to 10. This is the value of the node whose inorder successor you want to find.
DSA Javascript
Hint
Just create a constant named targetValue and assign it the number 10.
3
Implement the inorder successor function
Write a function called inorderSuccessor that takes root and targetValue as parameters. Use a loop to find the node with targetValue. Then find its inorder successor using BST rules: - If the node has a right child, the successor is the leftmost node in the right subtree. - Otherwise, move up the tree to find the nearest ancestor for which the node is in the left subtree. Return the successor node or null if none exists.
DSA Javascript
Hint
Use a loop to find the target node. Track a successor when moving left. If the node has a right child, find the leftmost node there. Otherwise, return the stored successor.
4
Print the inorder successor value
Call the inorderSuccessor function with root and targetValue. Then print the successor's value if it exists, or print null if there is no successor.
DSA Javascript
Hint
Call inorderSuccessor(root, targetValue) and store the result. Then print the value of the result if it exists, otherwise print null.
Practice
(1/5)
1. What is the inorder successor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest key greater than the given node's key
B. The node with the largest key smaller than the given node's key
C. The root node of the BST
D. The left child of the given node
Solution
Step 1: Understand inorder traversal order
Inorder traversal visits nodes in ascending order of their keys in a BST.
Step 2: Define inorder successor
The inorder successor of a node is the next node visited after it in inorder traversal, which is the smallest key greater than the node's key.
Final Answer:
The node with the smallest key greater than the given node's key -> Option A
Quick Check:
Inorder successor = next bigger key [OK]
Hint: Inorder successor is next bigger node in sorted order [OK]
Common Mistakes:
Confusing successor with predecessor
Thinking successor is always the right child
Assuming successor is the root node
2. Which of the following is the correct JavaScript function signature to find the inorder successor of a node in a BST?
easy
A. function inorderSuccessor(node) {}
B. function inorderSuccessor(root, node) {}
C. function findSuccessor(root) {}
D. function successor(node, root) {}
Solution
Step 1: Identify required inputs
To find the inorder successor, we need the BST root to traverse and the target node to find its successor.
Step 2: Match function signature
function inorderSuccessor(root, node) {} correctly takes both root and node as parameters, enabling traversal and comparison.
Final Answer:
function inorderSuccessor(root, node) {} -> Option B
Quick Check:
Function needs root and node [OK]
Hint: Successor function needs both root and target node [OK]
Common Mistakes:
Using only node without root
Wrong parameter order
Incorrect function naming
3. Given the BST below and the node with value 15, what is the inorder successor?
20
/ \
10 30
15
medium
A. 10
B. 30
C. 20
D. 15
Solution
Step 1: Locate node 15 and its right subtree
Node 15 has no right child, so successor is an ancestor.
Step 2: Find the lowest ancestor whose left child is also ancestor of 15
Node 20 is the first ancestor where 15 is in left subtree, so successor is 20.
Final Answer:
20 -> Option C
Quick Check:
Successor of 15 is 20 [OK]
Hint: If no right child, go up to first bigger ancestor [OK]
Common Mistakes:
Choosing right child when none exists
Picking the node itself as successor
Confusing successor with predecessor
4. Consider this buggy JavaScript code to find the inorder successor of a node in a BST:
function inorderSuccessor(root, node) {
if (!root) return null;
if (node.val >= root.val) {
return inorderSuccessor(root.right, node);
} else {
let left = inorderSuccessor(root.left, node);
return left ? left : root;
}
}
What is the main issue with this code?
medium
A. It does not handle the case when node has a right subtree
B. It incorrectly compares node.val with root.val
C. It returns the node itself instead of successor
D. It causes infinite recursion
Solution
Step 1: Analyze code logic
The code only traverses the tree comparing values but never checks if node has a right subtree.
Step 2: Identify missing right subtree check
If node has a right child, successor is the leftmost node in right subtree, which this code ignores.
Final Answer:
It does not handle the case when node has a right subtree -> Option A
Quick Check:
Missing right subtree check causes wrong successor [OK]
Hint: Check right subtree first for successor [OK]
Common Mistakes:
Ignoring right subtree
Wrong base case
Returning node instead of successor
5. You have a BST and want to find the inorder successor of node 25. The BST structure is:
20
/ \
10 30
/ \
25 40
What is the inorder successor of node 25 and why?
hard
A. 40, because 40 is the right child of 30
B. 20, because it is the root
C. None, because 25 is the largest node in its subtree
D. 30, because 25 has no right child and 30 is the next bigger ancestor
Solution
Step 1: Check if node 25 has right subtree
Node 25 has no right child, so successor is an ancestor.
Step 2: Find the lowest ancestor whose left subtree contains node 25
Node 30 is the parent of 25 and is the next bigger node, so successor is 30.
Final Answer:
30, because 25 has no right child and 30 is the next bigger ancestor -> Option D
Quick Check:
Successor of 25 is 30 [OK]
Hint: If no right child, successor is nearest bigger ancestor [OK]
Common Mistakes:
Choosing right child of parent instead of parent
Assuming no successor if no right child
Confusing root as successor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.