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?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class BST { public static void inorder(Node root) { if (root == null) return; inorder(root.left); System.out.print(root.val + " "); inorder(root.right); } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); inorder(root); } }
Attempts:
2 left
💡 Hint
Inorder traversal of a BST prints values in sorted order.
✗ Incorrect
Inorder traversal visits left subtree, then root, then right subtree. For BST, this results in sorted order.
🧠 Conceptual
intermediateWhy Does BST Property Matter for Search?
Why is the BST property important for efficient search operations?
Attempts:
2 left
💡 Hint
Think about how the BST property helps decide which subtree to search next.
✗ Incorrect
The BST property lets us decide to go left or right based on comparison, skipping half the nodes each time on average.
🔧 Debug
advancedIdentify the Bug Breaking BST Property
Which option shows code that breaks the BST property when inserting a node?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class BST { public static 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; } }
Attempts:
2 left
💡 Hint
Check which side the smaller values are inserted.
✗ Incorrect
Option C inserts smaller values to the right subtree, breaking BST property.
❓ Predict Output
advancedResult of Deleting a Node in BST
What is the inorder traversal output after deleting node 5 from this BST?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class BST { public static Node delete(Node root, int key) { if (root == null) return null; if (key < root.val) root.left = delete(root.left, key); else if (key > root.val) root.right = delete(root.right, key); else { if (root.left == null) return root.right; else if (root.right == null) return root.left; root.val = minValue(root.right); root.right = delete(root.right, root.val); } return root; } public static int minValue(Node root) { int minv = root.val; while (root.left != null) { root = root.left; minv = root.val; } return minv; } public static void inorder(Node root) { if (root == null) return; inorder(root.left); System.out.print(root.val + " "); inorder(root.right); } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); root = delete(root, 5); inorder(root); } }
Attempts:
2 left
💡 Hint
Deleting node with two children replaces it with smallest value in right subtree.
✗ Incorrect
Node 5 is replaced by 7 (min in right subtree), then 7 is deleted from original position.
🧠 Conceptual
expertWhy Can BST Become Inefficient Without Balancing?
Why can a BST become inefficient if it is not balanced?
Attempts:
2 left
💡 Hint
Think about the shape of the tree and how it affects search steps.
✗ Incorrect
If BST is skewed, it behaves like a linked list, losing the efficiency of binary search.
