BST Deletion: Leaf Node Case
What you'll learn: How to delete the simplest type of node in a Binary Search Tree—a leaf with no children.
The Simplest Deletion Scenario
When deleting a node from a BST, the easiest case is when the node you want to remove is a leaf node—a node with no left or right children. Think of it like pruning the tip of a tree branch: you simply snip it off, and nothing else is affected below it.
How It Works
To delete a leaf node:
- Find the node you want to delete (using BST search)
- Identify its parent node
- Update the parent's pointer (either
leftorright) tonull - The leaf node is now disconnected and can be garbage collected
For example, if you have a BST with values [5, 3, 7, 1, 4] and you want to delete the leaf 4:
- Node
4is the right child of node3 - Set
3.right = null - Node
4is now removed
Why This Is Simple
A leaf node has no dependents—no children that need to be relocated. You're not disrupting any subtrees or violating the BST property. The parent simply "forgets" about this child, and the rest of the tree remains valid.
Special Case: Deleting the Root
If the tree has only one node (the root, which is also a leaf), you simply set the root pointer to null, leaving you with an empty tree.
Key Takeaway: Deleting a leaf node from a BST is straightforward—just update the parent's pointer to null, disconnecting the leaf without affecting the BST structure or invariant.