2Brute2Best
πŸ”₯0

Delete Node in a BST

MediumΒ·LeetCode #450 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given the root of a binary search tree and a key.

Delete the node that holds key, then return the root of the tree. The tree must still be a valid BST. If key is not in the tree, the values stay the same. When the deleted node has two children, more than one shape can be correct, so any valid answer is accepted.

Input

The root of a binary search tree (between 0 and 10^4 nodes, all values unique), and an integer key.

Output

The root of a valid BST that holds the same values as before, minus key.

Example 1
Input: root = [5, 3, 6, 2, 4, null, 7], key = 3
Output: [5, 4, 6, 2, null, null, 7]

The node 3 has two children. Its inorder successor is 4, the smallest value bigger than 3. Copy 4 into that node, then remove the old 4. [5, 2, 6, null, 4, null, 7] is also accepted.

Example 2
Input: root = [5, 3, 6, 2, 4, null, 7], key = 0
Output: [5, 3, 6, 2, 4, null, 7]

No node holds 0, so nothing changes.

Example 3
Input: root = [], key = 0
Output: []

An empty tree stays empty.

Constraints
  • β€’ The number of nodes is between 0 and 10^4
  • β€’ βˆ’10^5 ≀ Node.val ≀ 10^5
  • β€’ Each node value is unique
  • β€’ root is a valid binary search tree
  • β€’ βˆ’10^5 ≀ key ≀ 10^5
  • β€’ Follow-up: can you do it in O(height of the tree) time?
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œdelete a node from a BST”
β†’ Find it with the BST rule, then fix the links so the order still holds.
β€œtwo children”
β†’ One value must replace the node: the inorder successor (or the predecessor).
β€œreturn the (possibly updated) root”
β†’ The parent must store whatever the recursive call returns.
03

Approach Complexity Ladder

Brute Force β€” collect the nodes, drop one, rebuild a balanced treeO(N)O(N)

Inorder list of the same node objects, drop the key's node, then relink them into a balanced BST

Optimal β€” walk to the key, then fix one pathO(H)O(H)

Find the key by comparing, then handle 0, 1 or 2 children; with 2 children, copy the successor up and delete it below

Brute Force Intuition

Walk the tree in order to collect the nodes. Remove the node whose value is key. Then rebuild a balanced BST from the sorted nodes that are left, changing only their child pointers. No new nodes are created, but every node is relinked, even the ones far from key.

Recognition rule for this sub-pattern:

Left < node < right. Inorder is sorted; a valid range (low, high) is passed down.
Remember this Β· Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.