Challenge - 5 Problems
BST Inorder Predecessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the inorder predecessor value in a BST
What is the output of the following Java code that finds the inorder predecessor of a node with value 15 in the BST?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class BSTPredecessor { static Node root; static 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; } public static void main(String[] args) { 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.left = new Node(25); root.right.right = new Node(35); Node pred = inorderPredecessor(root, 15); System.out.println(pred != null ? pred.val : "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 the largest value less than 15, so the output is 10.
❓ Predict Output
intermediateOutput when node has left subtree
What will be printed by this Java code that finds the inorder predecessor of node 20 in the BST?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class BSTPredecessor { static Node root; static Node findMax(Node node) { while (node.right != null) { node = node.right; } return node; } static Node inorderPredecessor(Node root, int key) { Node current = root; while (current != null && current.val != key) { if (key < current.val) { current = current.left; } else { current = current.right; } } if (current == null) return null; if (current.left != null) { return findMax(current.left); } Node predecessor = null; Node ancestor = root; while (ancestor != current) { if (current.val > ancestor.val) { predecessor = ancestor; ancestor = ancestor.right; } else { ancestor = ancestor.left; } } return predecessor; } public static void main(String[] args) { 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.left = new Node(25); root.right.right = new Node(35); Node pred = inorderPredecessor(root, 20); System.out.println(pred != null ? pred.val : "null"); } }
Attempts:
2 left
💡 Hint
If the node has a left subtree, the predecessor is the max in that subtree.
✗ Incorrect
Node 20 has a left child 10, which has a right child 15. The max in left subtree is 15, so the inorder predecessor is 15.
🔧 Debug
advancedIdentify the error in inorder predecessor code
What error will this Java code produce when trying to find the inorder predecessor of 10?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class BSTPredecessor { static Node root; static 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; } public static void main(String[] args) { 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); Node pred = inorderPredecessor(root, 10); System.out.println(pred != null ? pred.val : "null"); } }
Attempts:
2 left
💡 Hint
Check the left subtree of node 10 for predecessor.
✗ Incorrect
Node 10 has left child 5 and right child 15. The inorder predecessor is the max in left subtree, which is 5. No error occurs.
❓ Predict Output
advancedOutput when node has no left subtree
What will be the output of this Java code that finds the inorder predecessor of node 25 in the BST?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class BSTPredecessor { static Node root; static Node inorderPredecessor(Node root, int key) { Node current = root; Node predecessor = null; while (current != null) { if (key <= current.val) { current = current.left; } else { predecessor = current; current = current.right; } } return predecessor; } public static void main(String[] args) { 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.left = new Node(25); root.right.right = new Node(35); Node pred = inorderPredecessor(root, 25); System.out.println(pred != null ? pred.val : "null"); } }
Attempts:
2 left
💡 Hint
When no left subtree, predecessor is the last node smaller than key on path.
✗ Incorrect
Node 25 has no left child. The last node smaller than 25 on path is 20, so predecessor is 20.
🧠 Conceptual
expertUnderstanding inorder predecessor in BST
Which statement correctly describes the inorder predecessor of a node in a BST?
Attempts:
2 left
💡 Hint
Think about inorder traversal order and where predecessor lies.
✗ Incorrect
The inorder predecessor is the largest node smaller than the given node. If left subtree exists, it is the rightmost node there.
