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 C# code that finds the inorder successor of a node with value 15 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 Insert(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = Insert(root.left, val); else root.right = Insert(root.right, val); return root; } public Node MinValueNode(Node node) { Node current = node; while (current.left != null) current = current.left; return current; } public Node InorderSuccessor(Node root, Node n) { if (n.right != null) return MinValueNode(n.right); Node succ = null; while (root != null) { if (n.val < root.val) { succ = root; root = root.left; } else if (n.val > root.val) { root = root.right; } else break; } return succ; } } class Program { static void Main() { BST tree = new BST(); int[] values = {20, 8, 22, 4, 12, 10, 14, 15}; foreach (int v in values) tree.root = tree.Insert(tree.root, v); Node node = new Node(15); // Node to find successor for Node succ = tree.InorderSuccessor(tree.root, node); Console.WriteLine(succ != null ? succ.val.ToString() : "null"); } }
Attempts:
2 left
💡 Hint
Remember, if the node has a right child, the successor is the minimum value in the right subtree.
✗ Incorrect
The node with value 15 has no right child. So we look for the lowest ancestor for which 15 is in the left subtree. That ancestor is 20, so the inorder successor is 20.
❓ Predict Output
intermediateInorder successor when node has right subtree
What will be printed by this C# code that finds the inorder successor of node with value 8 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 Insert(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = Insert(root.left, val); else root.right = Insert(root.right, val); return root; } public Node MinValueNode(Node node) { Node current = node; while (current.left != null) current = current.left; return current; } public Node InorderSuccessor(Node root, Node n) { if (n.right != null) return MinValueNode(n.right); Node succ = null; while (root != null) { if (n.val < root.val) { succ = root; root = root.left; } else if (n.val > root.val) { root = root.right; } else break; } return succ; } } class Program { static void Main() { BST tree = new BST(); int[] values = {20, 8, 22, 4, 12, 10, 14}; foreach (int v in values) tree.root = tree.Insert(tree.root, v); Node node = new Node(8); // Node to find successor for Node succ = tree.InorderSuccessor(tree.root, node); Console.WriteLine(succ != null ? succ.val.ToString() : "null"); } }
Attempts:
2 left
💡 Hint
If the node has a right child, the successor is the smallest node in that right subtree.
✗ Incorrect
Node 8 has a right subtree with nodes 10, 12, 14. The smallest node there is 10, so the inorder successor is 10.
🔧 Debug
advancedIdentify the error in inorder successor code
What error will this C# code produce when trying to find the inorder successor of node 10?
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 Insert(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = Insert(root.left, val); else root.right = Insert(root.right, val); return root; } public Node MinValueNode(Node node) { Node current = node; while (current.left != null) current = current.left; return current; } public Node InorderSuccessor(Node root, Node n) { if (n.right != null) return MinValueNode(n.right); Node succ = null; while (root != null) { if (n.val <= root.val) { succ = root; root = root.left; } else { root = root.right; } } return succ; } } class Program { static void Main() { BST tree = new BST(); int[] values = {20, 8, 22, 4, 12, 10, 14}; foreach (int v in values) tree.root = tree.Insert(tree.root, v); Node node = new Node(10); Node succ = tree.InorderSuccessor(tree.root, node); Console.WriteLine(succ != null ? succ.val.ToString() : "null"); } }
Attempts:
2 left
💡 Hint
Check the condition inside the while loop for updating successor.
✗ Incorrect
The condition uses <= which causes the loop to move left even when n.val == root.val, skipping the correct path. The successor found is 20, which is correct for node 10 in this tree.
❓ Predict Output
advancedInorder successor of the maximum node
What will be the output of this C# code when finding the inorder successor of the node with value 22 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 Insert(Node root, int val) { if (root == null) return new Node(val); if (val < root.val) root.left = Insert(root.left, val); else root.right = Insert(root.right, val); return root; } public Node MinValueNode(Node node) { Node current = node; while (current.left != null) current = current.left; return current; } public Node InorderSuccessor(Node root, Node n) { if (n.right != null) return MinValueNode(n.right); Node succ = null; while (root != null) { if (n.val < root.val) { succ = root; root = root.left; } else if (n.val > root.val) { root = root.right; } else break; } return succ; } } class Program { static void Main() { BST tree = new BST(); int[] values = {20, 8, 22, 4, 12, 10, 14}; foreach (int v in values) tree.root = tree.Insert(tree.root, v); Node node = new Node(22); Node succ = tree.InorderSuccessor(tree.root, node); Console.WriteLine(succ != null ? succ.val.ToString() : "null"); } }
Attempts:
2 left
💡 Hint
The maximum node in BST has no inorder successor.
✗ Incorrect
Node 22 is the maximum node in the BST and has no right child, so its inorder successor is null.
🧠 Conceptual
expertInorder successor with parent pointer
Given a BST where each node has a parent pointer, which of the following is the correct logic to find the inorder successor of a node n?
Attempts:
2 left
💡 Hint
Think about inorder traversal and how parent pointers help find successor without root.
✗ Incorrect
Inorder successor is the smallest node greater than n. If n has right child, it's the leftmost node in right subtree. Otherwise, move up parents until you find a node which is left child of its parent; that parent is successor.
