测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A24034. 删除二叉排序树节点时,如果节点有两个孩子,则横线处应填入( ),其中 findMax 和 findMin 分别为找树的最大值和最小值。class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None def find_min(node): """在二叉搜索树中找到最小节点""" curre…

单选题 困难

题目描述

删除二叉排序树节点时,如果节点有两个孩子,则横线处应填入(    ),其中 findMax 和 findMin 分别为找树的最大值和最小值。

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

def find_min(node):
    """在二叉搜索树中找到最小节点"""
    current = node
    while current and current.left:
        current = current.left
    return current

def delete_node(root, key):
    if not root:
        return None

    if key < root.val:
        root.left = delete_node(root.left, key)
    elif key > root.val:
        root.right = delete_node(root.right, key)
    else:
        if not root.left:
            return root.right
        elif not root.right:
            return root.left
        else:
            temp = find_min(___________)

            root.val = temp.val
            root.right = delete_node(root.right, temp.val)

选项(单选)

上一题 下一题