Challenge - 5 Problems
BST Delete Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output after deleting a leaf node in BST
What is the printed state of the BST after deleting the leaf node with value 20?
DSA Go
package main import "fmt" type Node struct { val int left *Node right *Node } func insert(root *Node, val int) *Node { if root == nil { return &Node{val: val} } if val < root.val { root.left = insert(root.left, val) } else { root.right = insert(root.right, val) } return root } func inorder(root *Node) { if root != nil { inorder(root.left) fmt.Print(root.val, " -> ") inorder(root.right) } } func minValueNode(node *Node) *Node { current := node for current.left != nil { current = current.left } return current } func deleteNode(root *Node, key int) *Node { if root == nil { return root } if key < root.val { root.left = deleteNode(root.left, key) } else if key > root.val { root.right = deleteNode(root.right, key) } else { if root.left == nil { return root.right } else if root.right == nil { return root.left } temp := minValueNode(root.right) root.val = temp.val root.right = deleteNode(root.right, temp.val) } return root } func main() { var root *Node vals := []int{50, 30, 20, 40, 70, 60, 80} for _, v := range vals { root = insert(root, v) } root = deleteNode(root, 20) inorder(root) fmt.Print("null\n") }
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 sorted order without 20.
❓ Predict Output
intermediate2:00remaining
Output 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 Go
package main import "fmt" type Node struct { val int left *Node right *Node } func insert(root *Node, val int) *Node { if root == nil { return &Node{val: val} } if val < root.val { root.left = insert(root.left, val) } else { root.right = insert(root.right, val) } return root } func inorder(root *Node) { if root != nil { inorder(root.left) fmt.Print(root.val, " -> ") inorder(root.right) } } func minValueNode(node *Node) *Node { current := node for current.left != nil { current = current.left } return current } func deleteNode(root *Node, key int) *Node { if root == nil { return root } if key < root.val { root.left = deleteNode(root.left, key) } else if key > root.val { root.right = deleteNode(root.right, key) } else { if root.left == nil { return root.right } else if root.right == nil { return root.left } temp := minValueNode(root.right) root.val = temp.val root.right = deleteNode(root.right, temp.val) } return root } func main() { var root *Node vals := []int{50, 30, 20, 40, 70, 60, 80} for _, v := range vals { root = insert(root, v) } root = deleteNode(root, 30) inorder(root) fmt.Print("null\n") }
Attempts:
2 left
💡 Hint
Deleting a node with one child replaces it with its child.
✗ Incorrect
Node 30 has two children, but after deletion, its left child 20 and right child 40 remain. The node 30 is replaced by its right child 40, and 20 remains as left child of 40.
❓ Predict Output
advanced2:00remaining
Output 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 Go
package main import "fmt" type Node struct { val int left *Node right *Node } func insert(root *Node, val int) *Node { if root == nil { return &Node{val: val} } if val < root.val { root.left = insert(root.left, val) } else { root.right = insert(root.right, val) } return root } func inorder(root *Node) { if root != nil { inorder(root.left) fmt.Print(root.val, " -> ") inorder(root.right) } } func minValueNode(node *Node) *Node { current := node for current.left != nil { current = current.left } return current } func deleteNode(root *Node, key int) *Node { if root == nil { return root } if key < root.val { root.left = deleteNode(root.left, key) } else if key > root.val { root.right = deleteNode(root.right, key) } else { if root.left == nil { return root.right } else if root.right == nil { return root.left } temp := minValueNode(root.right) root.val = temp.val root.right = deleteNode(root.right, temp.val) } return root } func main() { var root *Node vals := []int{50, 30, 20, 40, 70, 60, 80} for _, v := range vals { root = insert(root, v) } root = deleteNode(root, 50) inorder(root) fmt.Print("null\n") }
Attempts:
2 left
💡 Hint
Deleting a node with two children replaces it with the smallest node in the right subtree.
✗ Incorrect
Node 50 is replaced by 60 (smallest in right subtree). Then 60 is deleted from its original position.
🔧 Debug
advanced2:00remaining
Identify the error in BST delete function
What error will occur when running this BST delete function if the node to delete has no children?
DSA Go
func deleteNode(root *Node, key int) *Node { if root == nil { return root } if key < root.val { root.left = deleteNode(root.left, key) } else if key > root.val { root.right = deleteNode(root.right, key) } else { if root.left == nil { return root.right } else if root.right == nil { return root.left } temp := minValueNode(root.right) root.val = temp.val root.right = deleteNode(root.right, temp.val) } return root } // Assume minValueNode is correctly implemented.
Attempts:
2 left
💡 Hint
Check how leaf nodes are handled in the code.
✗ Incorrect
The function correctly returns nil when deleting a leaf node (both children nil). No error occurs.
🧠 Conceptual
expert2:00remaining
Minimum number of nodes in BST after deleting root with two children
Given a BST with 7 nodes inserted in this order: 50, 30, 20, 40, 70, 60, 80. After deleting the root node (50), what is the minimum number of nodes remaining 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 BST had 7 nodes, so 6 remain.