You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

二叉搜索树递归删除方法问题:根节点替换异常求助

Fixing BST Recursive Delete: Root Node Replacement Issue

Hey there! Let's dig into this BST recursive delete issue you're having with the root node. It sounds like the classic pitfall where we're not properly updating the root reference or mixing up the order of operations when replacing the root with the right subtree's minimum value.

Core Problem Breakdown

When deleting the root node, the correct workflow should be:

  1. Locate the minimum node in the root's right subtree (min_right).
  2. Replace the root's value with min_right's value.
  3. Delete the min_right node from the right subtree (since its value has been moved up to the root).

The most common mistakes here are:

  • Deleting the min_right node before copying its value to the root (losing the value we need to replace with).
  • Failing to properly reassign the root reference after modifying the subtree in recursive calls.

Corrected Recursive Delete Implementation

Assuming your BST node class has val, left, and right attributes, here's a fixed version of the remove function:

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def remove(root, key):
    if not root:
        return root  # Key not found, return unchanged tree
    
    # Recursively navigate to the node to delete
    if key < root.val:
        root.left = remove(root.left, key)
    elif key > root.val:
        root.right = remove(root.right, key)
    else:
        # We've found the node to delete (could be the root)
        # Case 1: Node has 0 or 1 child
        if not root.left:
            return root.right
        elif not root.right:
            return root.left
        
        # Case 2: Node has two children - replace with right subtree's min
        # Step 1: Find the smallest node in the right subtree
        min_right = root.right
        while min_right.left:
            min_right = min_right.left
        
        # Step 2: Copy the min value to the current node (root, in this scenario)
        root.val = min_right.val
        
        # Step 3: Delete the min node from the right subtree
        root.right = remove(root.right, min_right.val)
    
    return root

Critical Fixes for Root Deletion

  • Copy first, delete later: We update the root's value before recursively deleting the min_right node. This ensures we don't lose the value we need to replace the root with.
  • Proper subtree reassignment: When deleting the min_right node, we reassign root.right to the result of the recursive call. This maintains the correct tree structure after removing the min node.
  • Capture the returned root: When calling remove on the root, make sure to reassign the root variable:
    root = remove(root, root.val)  # Correctly updates the root reference
    
    If you skip this reassignment, the root might not reflect the changes from the recursive call.

If your original code was deleting the min_right node before copying its value, that explains exactly why the root's value wasn't updated and why the min node was incorrectly removed.

内容的提问来源于stack exchange,提问作者nessa.c

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:46:51