Challenge - 5 Problems
BST Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Inorder Traversal on a BST
What is the output of the inorder traversal of this BST after inserting the values 10, 5, 15, 3, 7, 12, 18 in that order?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return Node(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root def inorder(root): return inorder(root.left) + [root.val] + inorder(root.right) if root else [] root = None for v in [10, 5, 15, 3, 7, 12, 18]: root = insert(root, v) print(inorder(root))
Attempts:
2 left
💡 Hint
Inorder traversal of a BST visits nodes in ascending order.
✗ Incorrect
Inorder traversal visits left subtree, then root, then right subtree. For BST, this produces sorted values.
🧠 Conceptual
intermediateWhy Does BST Property Matter for Search?
Why is the BST property important for efficient searching in a binary search tree?
Attempts:
2 left
💡 Hint
Think about how the tree structure helps find values faster than checking every node.
✗ Incorrect
The BST property lets us decide to go left or right at each node, skipping half the nodes on average, leading to O(log n) search time.
🔧 Debug
advancedIdentify the Bug in BST Insertion
What error will this BST insertion code cause when inserting duplicate values?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return Node(val) if val < root.val: root.left = insert(root.left, val) elif val > root.val: root.right = insert(root.right, val) # No else case for val == root.val return root root = None for v in [10, 5, 15, 10]: root = insert(root, v)
Attempts:
2 left
💡 Hint
Check what happens when val equals root.val in the insert function.
✗ Incorrect
Since no action is taken when val == root.val, duplicates are ignored and not inserted.
❓ Predict Output
advancedOutput After Deleting a Node in BST
What is the inorder traversal output after deleting the node with value 10 from this BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, val): if root is None: return Node(val) if val < root.val: root.left = insert(root.left, val) else: root.right = insert(root.right, val) return root def inorder(root): return inorder(root.left) + [root.val] + inorder(root.right) if root else [] def find_min(root): while root.left: root = root.left return root def delete(root, val): if root is None: return root if val < root.val: root.left = delete(root.left, val) elif val > root.val: root.right = delete(root.right, val) else: if root.left is None: return root.right elif root.right is None: return root.left temp = find_min(root.right) root.val = temp.val root.right = delete(root.right, temp.val) return root root = None for v in [10, 5, 15, 3, 7, 12, 18]: root = insert(root, v) root = delete(root, 10) print(inorder(root))
Attempts:
2 left
💡 Hint
When deleting a node with two children, replace it with the smallest value in the right subtree.
✗ Incorrect
Deleting 10 replaces it with 12 (smallest in right subtree), then deletes 12 from right subtree. Inorder traversal reflects this.
🧠 Conceptual
expertWhy BST Property is Crucial for Balanced Trees
Which statement best explains why maintaining the BST property is crucial when balancing a binary search tree?
Attempts:
2 left
💡 Hint
Think about what happens to node order after tree rotations.
✗ Incorrect
Rotations used in balancing must keep the BST property intact so the tree remains correctly ordered for searching.
