LeetCode 题解工作台
删除二叉搜索树中的节点
给定一个二叉搜索树的根节点 root 和一个值 key ,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。 一般来说,删除节点可分为两个步骤: 首先找到需要删除的节点; 如果找到了,删除它。 示例 1: 输入: root = [5,3…
3
题型
6
代码语言
3
相关题
当前训练重点
中等 · 二分·树·traversal
答案摘要
二叉搜索树有以下性质: 1. 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路
题目描述
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
一般来说,删除节点可分为两个步骤:
- 首先找到需要删除的节点;
- 如果找到了,删除它。
示例 1:

输入:root = [5,3,6,2,4,null,7], key = 3 输出:[5,4,6,2,null,null,7] 解释:给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。 一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。 另一个正确答案是 [5,2,6,null,4,null,7]。![]()
示例 2:
输入: root = [5,3,6,2,4,null,7], key = 0 输出: [5,3,6,2,4,null,7] 解释: 二叉树不包含值为 0 的节点
示例 3:
输入: root = [], key = 0 输出: []
提示:
- 节点数的范围
[0, 104]. -105 <= Node.val <= 105- 节点值唯一
root是合法的二叉搜索树-105 <= key <= 105
进阶: 要求算法时间复杂度为 O(h),h 为树的高度。
解题思路
方法一:递归
二叉搜索树有以下性质:
- 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
- 若任意节点的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
- 任意节点的左、右子树也分别为二叉搜索树。
我们可以递归判断当前节点 与 的大小关系:
- 若 ,则递归左子树;
- 若 ,则递归右子树;
- 若 ,则进一步判断:
- 若 没有左子树,则 顶替 的位置;
- 若 没有右子树,则 顶替 的位置;
- 若 同时存在左右子树,则将左子树转移至右子树的最左节点的左子树上,然后 顶替 的位置。
时间复杂度 ,其中 是树的高度。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
if root is None:
return None
if root.val > key:
root.left = self.deleteNode(root.left, key)
return root
if root.val < key:
root.right = self.deleteNode(root.right, key)
return root
if root.left is None:
return root.right
if root.right is None:
return root.left
node = root.right
while node.left:
node = node.left
node.left = root.left
root = root.right
return root
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
They want to see whether you separate the search phase from the subtree-rewiring phase instead of mixing cases too early.
- question_mark
They are checking whether you know why the inorder successor or predecessor preserves BST order for the two-child delete case.
- question_mark
They care about whether your function returns the new subtree root correctly, especially when deleting the original root node.
常见陷阱
外企场景- error
Replacing a two-child node's value with its successor but forgetting to delete the successor node, which leaves a duplicate in the BST.
- error
Updating the child pointer locally but not returning it upward, causing the parent link or root replacement to stay stale.
- error
Using a generic binary-tree delete idea that ignores BST ordering and accidentally breaks the in-order sorted property.
进阶变体
外企场景- arrow_right_alt
Use the inorder predecessor from the left subtree instead of the successor from the right subtree for the two-child case.
- arrow_right_alt
Write the deletion iteratively by tracking the parent pointer, which removes recursion but makes edge handling around the root more manual.
- arrow_right_alt
Augment the BST with parent pointers or subtree metadata, where deletion still follows the same cases but requires extra state updates after relinking.