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 code that finds the inorder predecessor of node 15 in the given BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def inorder_predecessor(root, key): predecessor = None current = root while current: if key <= current.val: current = current.left else: predecessor = current current = current.right return predecessor.val if predecessor else None # BST construction root = Node(20) root.left = Node(10) root.right = Node(30) root.left.left = Node(5) root.left.right = Node(15) root.left.right.left = Node(12) root.left.right.right = Node(17) print(inorder_predecessor(root, 15))
Attempts:
2 left
💡 Hint
Remember, the inorder predecessor is the largest value smaller than the given key.
✗ Incorrect
The inorder predecessor of 15 is the maximum value smaller than 15 in the BST. Node 12 is the rightmost node in the left subtree of 15, so it is the predecessor.
🧠 Conceptual
intermediateUnderstanding inorder predecessor in BST
Which statement correctly describes the inorder predecessor of a node in a BST?
Attempts:
2 left
💡 Hint
Think about the inorder traversal order of BST nodes.
✗ Incorrect
Inorder traversal visits nodes in ascending order. The predecessor is the node visited just before the given node, so it must be the largest smaller value.
🔧 Debug
advancedIdentify the error in inorder predecessor function
What error will the following code produce when trying to find the inorder predecessor of 5 in the BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def inorder_predecessor_buggy(root, key): predecessor = None current = root while current: if key < current.val: current = current.left elif key > current.val: predecessor = current current = current.right else: if current.left: temp = current.left while temp.right: temp = temp.right return temp.val break return predecessor.val if predecessor else None root = Node(20) root.left = Node(10) root.right = Node(30) root.left.left = Node(5) root.left.right = Node(15) print(inorder_predecessor_buggy(root, 5))
Attempts:
2 left
💡 Hint
Check what happens when predecessor is None and you try to access its val.
✗ Incorrect
If the node has no left subtree and predecessor is None, the code tries to return predecessor.val causing AttributeError.
❓ Predict Output
advancedOutput of inorder predecessor for root node
What is the output of the code when finding the inorder predecessor of the root node (20) in the BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def inorder_predecessor(root, key): predecessor = None current = root while current: if key <= current.val: current = current.left else: predecessor = current current = current.right return predecessor.val if predecessor else None root = Node(20) root.left = Node(10) root.right = Node(30) root.left.left = Node(5) root.left.right = Node(15) print(inorder_predecessor(root, 20))
Attempts:
2 left
💡 Hint
The predecessor is the largest value smaller than 20.
✗ Incorrect
The largest value smaller than 20 in the BST is 15, so the predecessor is 15.
🚀 Application
expertFind inorder predecessor in BST with parent pointers
Given a BST node with a parent pointer, which approach correctly finds its inorder predecessor?
Attempts:
2 left
💡 Hint
Think about inorder traversal and how parent pointers help find predecessor.
✗ Incorrect
If node has left subtree, predecessor is max in left subtree. Otherwise, move up until node is right child of parent.
