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 2?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, key): if root is None: return Node(key) if key < root.val: root.left = insert(root.left, key) else: root.right = insert(root.right, key) return root def inorder(root, res): if root: inorder(root.left, res) res.append(root.val) inorder(root.right, res) def deleteNode(root, key): if root is None: return root if key < root.val: root.left = deleteNode(root.left, key) elif key > root.val: root.right = deleteNode(root.right, key) else: if root.left is None: return root.right elif root.right is None: return root.left temp = root.right while temp.left: temp = temp.left root.val = temp.val root.right = deleteNode(root.right, temp.val) return root root = None for v in [5, 3, 7, 2, 4]: root = insert(root, v) root = deleteNode(root, 2) result = [] inorder(root, result) print(result)
Attempts:
2 left
💡 Hint
Deleting a leaf node removes it without affecting other nodes.
✗ Incorrect
Node 2 is a leaf and gets removed. The inorder traversal then shows [3, 4, 5, 7].
❓ 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 3 which has one child?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, key): if root is None: return Node(key) if key < root.val: root.left = insert(root.left, key) else: root.right = insert(root.right, key) return root def inorder(root, res): if root: inorder(root.left, res) res.append(root.val) inorder(root.right, res) def deleteNode(root, key): if root is None: return root if key < root.val: root.left = deleteNode(root.left, key) elif key > root.val: root.right = deleteNode(root.right, key) else: if root.left is None: return root.right elif root.right is None: return root.left temp = root.right while temp.left: temp = temp.left root.val = temp.val root.right = deleteNode(root.right, temp.val) return root root = None for v in [5, 3, 7, 4]: root = insert(root, v) root = deleteNode(root, 3) result = [] inorder(root, result) print(result)
Attempts:
2 left
💡 Hint
Deleting a node with one child replaces it with its child.
✗ Incorrect
Node 3 has one child 4. After deletion, 4 takes 3's place. Inorder traversal is [4, 5, 7].
❓ 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 3 which has two children?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def insert(root, key): if root is None: return Node(key) if key < root.val: root.left = insert(root.left, key) else: root.right = insert(root.right, key) return root def inorder(root, res): if root: inorder(root.left, res) res.append(root.val) inorder(root.right, res) def deleteNode(root, key): if root is None: return root if key < root.val: root.left = deleteNode(root.left, key) elif key > root.val: root.right = deleteNode(root.right, key) else: if root.left is None: return root.right elif root.right is None: return root.left temp = root.right while temp.left: temp = temp.left root.val = temp.val root.right = deleteNode(root.right, temp.val) return root root = None for v in [5, 3, 7, 2, 4]: root = insert(root, v) root = deleteNode(root, 3) result = [] inorder(root, result) print(result)
Attempts:
2 left
💡 Hint
Deleting a node with two children replaces it with its inorder successor.
✗ Incorrect
Node 3 has two children. It is replaced by its inorder successor 4. Inorder traversal is [2, 4, 5, 7].
🔧 Debug
advancedIdentify the error in BST delete function
What error will this code produce when deleting a node with two children?
DSA Python
def deleteNode(root, key): if root is None: return root if key < root.val: root.left = deleteNode(root.left, key) elif key > root.val: root.right = deleteNode(root.right, key) else: if root.left is None: return root.right elif root.right is None: return root.left temp = root.right while temp.left: temp = temp.left root.val = temp.val root.right = deleteNode(root.right, temp.val) return root
Attempts:
2 left
💡 Hint
Check how the successor node is found in the right subtree.
✗ Incorrect
The code correctly finds the leftmost node in the right subtree (inorder successor). This is the correct approach.
🧠 Conceptual
expertNumber of nodes after multiple deletions in BST
Given a BST with nodes inserted in this order: [10, 5, 15, 3, 7, 12, 18], after deleting nodes 5 and 15, how many nodes remain in the BST?
Attempts:
2 left
💡 Hint
Each deletion removes exactly one node.
✗ Incorrect
Initially 7 nodes. Deleting 5 and 15 removes 2 nodes. Remaining nodes = 7 - 2 = 5.
