Challenge - 5 Problems
BST Inorder Predecessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Inorder Predecessor Search in BST
What is the output of the following C# code that finds the inorder predecessor of a node with value 15 in the given BST?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public Node InorderPredecessor(Node root, int key) { Node predecessor = null; Node current = root; while (current != null) { if (key <= current.val) { current = current.left; } else { predecessor = current; current = current.right; } } return predecessor; } } class Program { static void Main() { BST tree = new BST(); tree.root = new Node(20); tree.root.left = new Node(10); tree.root.right = new Node(30); tree.root.left.left = new Node(5); tree.root.left.right = new Node(15); tree.root.right.right = new Node(40); Node pred = tree.InorderPredecessor(tree.root, 15); Console.WriteLine(pred != null ? pred.val.ToString() : "null"); } }
Attempts:
2 left
💡 Hint
Remember, inorder predecessor is the largest value smaller than the given key.
✗ Incorrect
The inorder predecessor of 15 is the largest node smaller than 15. In the BST, 10 is smaller than 15 and is the closest smaller value. So the output is 10.
🧠 Conceptual
intermediateUnderstanding Inorder Predecessor in BST
In a Binary Search Tree (BST), which of the following best describes the inorder predecessor of a node with value X?
Attempts:
2 left
💡 Hint
Think about the inorder traversal order and what comes just before X.
✗ Incorrect
Inorder traversal visits nodes in ascending order. The inorder predecessor of X is the node visited just before X, which is the largest value smaller than X.
❓ Predict Output
advancedOutput of Inorder Predecessor with Node Having Left Subtree
What will be the output of the following code that finds the inorder predecessor of node with value 20 in the BST?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public Node FindNode(Node root, int key) { if (root == null) return null; if (root.val == key) return root; if (key < root.val) return FindNode(root.left, key); else return FindNode(root.right, key); } public Node InorderPredecessor(Node root, int key) { Node target = FindNode(root, key); if (target == null) return null; if (target.left != null) { Node temp = target.left; while (temp.right != null) temp = temp.right; return temp; } Node predecessor = null; Node current = root; while (current != null) { if (key > current.val) { predecessor = current; current = current.right; } else if (key < current.val) { current = current.left; } else { break; } } return predecessor; } } class Program { static void Main() { BST tree = new BST(); tree.root = new Node(20); tree.root.left = new Node(10); tree.root.right = new Node(30); tree.root.left.left = new Node(5); tree.root.left.right = new Node(15); tree.root.left.right.left = new Node(12); Node pred = tree.InorderPredecessor(tree.root, 20); Console.WriteLine(pred != null ? pred.val.ToString() : "null"); } }
Attempts:
2 left
💡 Hint
If the node has a left subtree, the predecessor is the rightmost node in that subtree.
✗ Incorrect
Node 20 has a left child 10. The rightmost node in 10's subtree is 15, so 15 is the inorder predecessor.
🔧 Debug
advancedIdentify the Error in Inorder Predecessor Code
What error will the following code produce when trying to find the inorder predecessor of 10 in the BST?
DSA C#
using System; class Node { public int val; public Node left, right; public Node(int v) { val = v; left = right = null; } } class BST { public Node root; public Node InorderPredecessor(Node root, int key) { Node predecessor = null; Node current = root; while (current != null) { if (key < current.val) { current = current.left; } else if (key > current.val) { predecessor = current; current = current.right; } else { if (current.left != null) { Node temp = current.left; while (temp.right != null) temp = temp.right; return temp; } break; } } return predecessor; } } class Program { static void Main() { BST tree = new BST(); tree.root = new Node(20); tree.root.left = new Node(10); tree.root.right = new Node(30); tree.root.right.left = new Node(25); tree.root.right.right = new Node(40); Node pred = tree.InorderPredecessor(tree.root, 10); if (pred != null) { Console.WriteLine(pred.val); } else { Console.WriteLine("null"); } } }
Attempts:
2 left
💡 Hint
Check what happens if predecessor is null before accessing its val.
✗ Incorrect
If the predecessor is null, accessing pred.val causes NullReferenceException. Here, predecessor is null because 10 has no left subtree and no smaller ancestor.
🚀 Application
expertNumber of Nodes in Result of Inorder Predecessor Search
Given a BST with nodes [50, 30, 70, 20, 40, 60, 80], how many nodes will be visited in total when searching for the inorder predecessor of 65 using the standard BST inorder predecessor algorithm?
Attempts:
2 left
💡 Hint
Trace the path from root to the node or where the node would be inserted.
✗ Incorrect
Searching for 65, path is 50 -> 70 -> 60 (since 65 < 70, go left). Nodes visited: 50, 70, 60. Total 3 nodes.
