NeetCode #423LC-450MediumTreesNC 250
← Back to All Problems#423 · #450 · Delete Node in a BST(删除二叉搜索树中的节点)
📌 Problem Statement & Constraints
Given the root of a binary search tree and a key, delete the node with that key and return the root. The tree must remain a valid BST. Constraints: the number of nodes is in
[0, 10^4], -10^5 <= Node.val <= 10^5; values are unique.💡 Core Algorithmic Approaches
- Search for the key as in a normal BST descent.
- Once found, there are three cases: no left child (return the right), no right child (return the left), and two children.
- For two children, replace the node's value with its in-order successor (the leftmost node of the right subtree) and then delete that successor from the right subtree.
- Returning the (possibly new) subtree root from each call handles the relinking automatically.
💻 Benchmark Python3 Implementation
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if not root:
return None
if key < root.val:
root.left = self.deleteNode(root.left, key)
elif key > root.val:
root.right = self.deleteNode(root.right, key)
else:
# found the node to delete
if not root.left:
return root.right # no left child
if not root.right:
return root.left # no right child
# two children: replace with the in-order successor
succ = root.right
while succ.left:
succ = succ.left # leftmost of the right subtree
root.val = succ.val
root.right = self.deleteNode(root.right, succ.val)
return root⚡ Complexity Deep Dive
⏱️ Time Complexity
O(h): the search plus the successor lookup each follow one path.
💾 Space Complexity
O(h) for the recursion.
⚠️ Interview Pitfalls & Follow-ups
- Using the in-order predecessor instead: also correct, but pick one and be consistent -- the successor is the conventional choice.
- Forgetting to delete the successor after copying its value: the tree would contain a duplicate.
- Returning
Nonefor a node with one child: the existing child must be returned so the parent relinks correctly. - Deleting from the left subtree when the successor came from the right: the recursive delete must target
root.right.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🧩 Related Trees Problems
中文速览与面试要点
删除二叉搜索树中的节点
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。一般来说,删除节点可分为两个步骤:首先找到需要删除的节点;如果找到了,删除它。约束:节点数的范围 [0, 10^4];-10^5 ≤ Node.val ≤ 10^5;节点值 唯一 ;-10^5 ≤ key ≤ 10^5;题目数据保证需要删除的节点 存在 于树中。
核心要点
- 递归删除(标准解) :先按 BST 性质递归找到目标节点,再分三种情况处理:
- ① 无左子节点 :直接用右子节点顶替(返回 root.right )。
- ② 无右子节点 :直接用左子节点顶替(返回 root.left )。
- ③ 左右都有 :找右子树的 最小节点 (中序后继),把它的值复制到当前节点,然后递归删除该后继节点。
- 为什么用中序后继 :右子树的最小值恰好是「大于当前值的最小值」,用它顶替可以保持 BST 性质。也可以用左子树的最大值(中序前驱)。
常见陷阱
- 忘记用 root.left = ... 接住递归返回值 :删除后子树可能换了根,必须重新挂接。
- 情况 ③ 中忘记递归删除后继节点 :会导致树中出现两个相同的值。
- 情况 ③ 中直接返回 root.right :会丢失右子树中除后继以外的所有节点。
- 情况 ①② 的顺序颠倒 :先判断 not root.right 再判断 not root.left ,结果相同(都是「用另一侧顶替」),但要保证「无左用右、无右用左」的对应关系。
- 找后继时写成找左子树的最大值 :那是中序前驱,也可以,但要注意后续删除逻辑相应调整。
参考实现
class Solution:
def deleteNode(self, root: 'TreeNode', key: int) -> 'TreeNode':
if not root:
return None
if key < root.val:
root.left = self.deleteNode(root.left, key)
elif key > root.val:
root.right = self.deleteNode(root.right, key)
else:
# 找到目标节点
if not root.left:
return root.right # 情况 ①
if not root.right:
return root.left # 情况 ②
# 情况 ③:用右子树的最小节点(中序后继)顶替
succ = root.right
while succ.left:
succ = succ.left
root.val = succ.val
root.right = self.deleteNode(root.right, succ.val)
return root