Deletion in a Binary Search Tree
Separate the leaf, one-child, and two-child cases and check the ordering after deletion.
Core Intuition & How It Works
Deleting a key from a BST handles three cases: removing a leaf directly, bypassing a node with one child, or replacing a two-child node with its inorder successor (smallest key in its right subtree) and deleting that successor.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(h) |
| Space Complexity | O(h) recursive, O(1) iterative |
| Topic Chapter | Chapter 7: Trees |
| Difficulty Level | Intermediate |
Key Pattern to Recognize
For the two-child case, swap or copy the inorder successor’s value, then recursively delete that successor from the right subtree.
Common Pitfalls & Edge Cases
Dropping the successor’s right subtree when unlinking it, or failing to reattach the updated child pointer to the parent.
Real-World & Technical Interview Applications
Dynamic ordered sets, in-memory indexes, and maintaining self-balancing trees.