Challenge - 5 Problems
BST Inorder Successor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the inorder successor of a node in BST
What is the output of the following code that finds the inorder successor of node 15 in the BST?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class BSTSuccessor { public static Node inorderSuccessor(Node root, Node p) { Node successor = null; while (root != null) { if (p.val < root.val) { successor = root; root = root.left; } else { root = root.right; } } return successor; } public static void main(String[] args) { Node 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); root.right.right = new Node(40); Node p = root.left.right; // Node with value 15 Node succ = inorderSuccessor(root, p); System.out.println(succ != null ? succ.val : "null"); } }
Attempts:
2 left
💡 Hint
Remember inorder successor is the next bigger node in BST.
✗ Incorrect
The inorder successor of 15 is 20 because 20 is the smallest node greater than 15 in the BST.
❓ Predict Output
intermediateInorder successor when node has right subtree
What will be printed by this code when finding the inorder successor of node 10?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class BSTSuccessor { public static Node leftMost(Node node) { while (node.left != null) { node = node.left; } return node; } public static Node inorderSuccessor(Node root, Node p) { if (p.right != null) { return leftMost(p.right); } Node successor = null; while (root != null) { if (p.val < root.val) { successor = root; root = root.left; } else if (p.val > root.val) { root = root.right; } else { break; } } return successor; } public static void main(String[] args) { Node 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); root.right.right = new Node(40); Node p = root.left; // Node with value 10 Node succ = inorderSuccessor(root, p); System.out.println(succ != null ? succ.val : "null"); } }
Attempts:
2 left
💡 Hint
If node has right child, successor is leftmost node in right subtree.
✗ Incorrect
Node 10 has right child 15, so the inorder successor is the leftmost node in 10's right subtree, which is 15.
🧠 Conceptual
advancedUnderstanding inorder successor when no right subtree
In a BST, if a node has no right subtree, how is its inorder successor determined?
Attempts:
2 left
💡 Hint
Think about climbing up the tree to find a node bigger than current.
✗ Incorrect
If no right subtree, inorder successor is the lowest ancestor for which the node lies in its left subtree.
🔧 Debug
advancedIdentify the error in inorder successor code
What error will this code produce when trying to find the inorder successor of node 25?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class BSTSuccessor { public static Node inorderSuccessor(Node root, Node p) { if (p.right != null) { Node curr = p.right; while (curr.left != null) { curr = curr.left; } return curr; } Node successor = null; while (root != null) { if (p.val < root.val) { successor = root; root = root.left; } else { root = root.right; } } return successor; } public static void main(String[] args) { Node root = new Node(20); root.left = new Node(10); root.right = new Node(30); root.right.left = new Node(25); root.right.right = new Node(40); Node p = root.right.left; // Node with value 25 Node succ = inorderSuccessor(root, p); System.out.println(succ != null ? succ.val : "null"); } }
Attempts:
2 left
💡 Hint
Check the condition p.val <= root.val in the while loop.
✗ Incorrect
The code prints 25, which is incorrect. The correct successor is 30, but the <= condition causes successor to be set to p itself when root reaches p.
🚀 Application
expertCount nodes with no inorder successor in BST
Given a BST, how many nodes have no inorder successor? Consider the BST below:
20
/ \
10 30
/ \ \
5 15 40
Select the correct count.
Attempts:
2 left
💡 Hint
Only the maximum node in the BST has no inorder successor.
✗ Incorrect
Only node 40 has no inorder successor because it is the largest node (rightmost in inorder traversal).
