Challenge - 5 Problems
BST Delete Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting a leaf node in BST
What is the printed state of the BST after deleting the leaf node with value 20?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } class BST { Node root; 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; } Node delete(Node root, int val) { if (root == null) return null; if (val < root.val) root.left = delete(root.left, val); else if (val > root.val) root.right = delete(root.right, val); else { if (root.left == null && root.right == null) return null; else if (root.left == null) return root.right; else if (root.right == null) return root.left; else { Node succ = root.right; while (succ.left != null) succ = succ.left; root.val = succ.val; root.right = delete(root.right, succ.val); } } return root; } 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) { BST tree = new BST(); int[] vals = {50, 30, 70, 20, 40, 60, 80}; for (int v : vals) tree.root = tree.insert(tree.root, v); tree.root = tree.delete(tree.root, 20); tree.inorder(tree.root); System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Deleting a leaf node removes it without affecting other nodes.
✗ Incorrect
Deleting 20 removes the leaf node 20. The inorder traversal prints nodes in ascending order without 20.
❓ Predict Output
intermediateOutput after deleting a node with one child in BST
What is the printed state of the BST after deleting the node with value 30, which has one child?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } class BST { Node root; 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; } Node delete(Node root, int val) { if (root == null) return null; if (val < root.val) root.left = delete(root.left, val); else if (val > root.val) root.right = delete(root.right, val); else { if (root.left == null && root.right == null) return null; else if (root.left == null) return root.right; else if (root.right == null) return root.left; else { Node succ = root.right; while (succ.left != null) succ = succ.left; root.val = succ.val; root.right = delete(root.right, succ.val); } } return root; } 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) { BST tree = new BST(); int[] vals = {50, 30, 70, 40, 60, 80}; for (int v : vals) tree.root = tree.insert(tree.root, v); tree.root = tree.delete(tree.root, 30); tree.inorder(tree.root); System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Deleting a node with one child replaces it with its child.
✗ Incorrect
Node 30 has one child 40. After deletion, 40 replaces 30. Inorder traversal reflects this.
❓ Predict Output
advancedOutput after deleting a node with two children in BST
What is the printed state of the BST after deleting the node with value 50, which has two children?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } class BST { Node root; 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; } Node delete(Node root, int val) { if (root == null) return null; if (val < root.val) root.left = delete(root.left, val); else if (val > root.val) root.right = delete(root.right, val); else { if (root.left == null && root.right == null) return null; else if (root.left == null) return root.right; else if (root.right == null) return root.left; else { Node succ = root.right; while (succ.left != null) succ = succ.left; root.val = succ.val; root.right = delete(root.right, succ.val); } } return root; } 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) { BST tree = new BST(); int[] vals = {50, 30, 70, 20, 40, 60, 80}; for (int v : vals) tree.root = tree.insert(tree.root, v); tree.root = tree.delete(tree.root, 50); tree.inorder(tree.root); System.out.print("null"); } }
Attempts:
2 left
💡 Hint
Deleting a node with two children replaces it with its inorder successor.
✗ Incorrect
Node 50 is replaced by its inorder successor 60. Then 60 is deleted from right subtree. Inorder traversal reflects this.
🔧 Debug
advancedIdentify the error in BST delete method
What error will occur when running this BST delete method if the node to delete has two children?
DSA Java
Node delete(Node root, int val) { if (root == null) return null; if (val < root.val) root.left = delete(root.left, val); else if (val > root.val) root.right = delete(root.right, val); else { if (root.left == null && root.right == null) return null; else if (root.left == null) return root.right; else if (root.right == null) return root.left; else { Node succ = root.right; while (succ.left != null) succ = succ.left; root.val = succ.val; // Missing recursive delete call here } } return root; }
Attempts:
2 left
💡 Hint
Check if the inorder successor is removed after copying its value.
✗ Incorrect
The code copies the inorder successor's value but does not delete the successor node from the right subtree, causing duplicate values in the BST.
🧠 Conceptual
expertMinimum number of nodes in BST after deleting root with two children
Given a BST with 7 nodes, if the root node (which has two children) is deleted using the standard BST delete operation, how many nodes remain in the BST?
Attempts:
2 left
💡 Hint
Deleting a node removes exactly one node from the tree.
✗ Incorrect
Deleting the root node removes one node. The inorder successor replaces the root's value and is then deleted from its original position, so total nodes decrease by one.
