leetcode450. Delete Node in a BST

栏目: 数据库 · 发布时间: 5年前

内容简介:假设有一棵二叉搜索树,现在要求从二叉搜索树中删除指定值,使得删除后的结果依然是一棵二叉搜索树。二叉搜索树的特点是,对于树中的任何一个节点,一定满足大于其所有左子节点值,小于所有其右子节点值。当删除二叉搜索树中的一个节点时,一共有三种场景:对每种情况的图例如下:

题目要求

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.

Basically, the deletion can be divided into two stages:

Search for a node to remove.
If the node is found, delete the node.
Note: Time complexity should be O(height of tree).

Example:

root = [5,3,6,2,4,null,7]
key = 3

    5
   / \
  3   6
 / \   \
2   4   7

Given key to delete is 3. So we find the node with value 3 and delete it.

One valid answer is [5,4,6,2,null,null,7], shown in the following BST.

    5
   / \
  4   6
 /     \
2       7

Another valid answer is [5,2,6,null,4,null,7].

    5
   / \
  2   6
   \   \
    4   7

假设有一棵二叉搜索树,现在要求从二叉搜索树中删除指定值,使得删除后的结果依然是一棵二叉搜索树。

思路和代码

二叉搜索树的特点是,对于树中的任何一个节点,一定满足大于其所有左子节点值,小于所有其右子节点值。当删除二叉搜索树中的一个节点时,一共有三种场景:

  1. 该该节点为叶节点,此时无需进行任何操作,直接删除该节点即可
  2. 该节点只有一个子树,则将唯一的直接子节点替换掉当前的节点即可
  3. 该节点既有做左子节点又有右子节点。这时候有两种选择,要么选择左子树的最大值,要么选择右子树的最小值填充至当前的节点,再递归的在子树中删除对应的最大值或是最小值。

对每种情况的图例如下:

1. 叶节点
    5
   / \
  2   6
   \   \
    4   7 (删除4)
结果为:
    5
   / \
  2   6
       \
        7
        
2. 只有左子树或是只有右子树
    5
   / \
  3   6
 / \   \
2   4   7(删除6)
结果为
    5
   / \
  3   6
 / \   
2   4   

3. 既有左子树又有右子树
    6
   / \
  3   7
 / \   \
2   5   8 (删除6)
   /
  4
首先找到6的左子树中的最大值为5,将5填充到6的位置
    5
   / \
  3   7
 / \   \
2   5   8 (删除5)
   /
  4
接着递归的在左子树中删除5,此时5满足只有一个子树的场景,因此直接用子树替换即可
    5
   / \
  3   7
 / \   \
2   4   8 (删除5)

代码如下:

public TreeNode deleteNode(TreeNode cur, int key) {
        if(cur == null) return null;
        else if(cur.val == key) {
            if(cur.left != null && cur.right != null) {
                TreeNode left = cur.left;
                while(left.right != null) {
                    left = left.right;
                }
                cur.val = left.val;
                cur.left = deleteNode(cur.left, left.val);
            }else if(cur.left != null) {
                return cur.left;
            }else if(cur.right != null){
                return cur.right;
            }else {
                return null;
            }
        }else if(cur.val > key) {
            cur.left = deleteNode(cur.left, key);
        }else {
            cur.right = deleteNode(cur.right, key);
        }
        return cur;
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数字化生存

数字化生存

(美)Nicholas Negroponte(尼古拉·尼葛洛庞帝) / 胡泳、范海燕 / 电子工业出版社 / 2017-1-1 / 68.00

《数字化生存》描绘了数字科技为我们的生活、工作、教育和娱乐带来的各种冲击和其中值得深思的问题,是跨入数字化新世界的*指南。英文版曾高居《纽约时报》畅销书排行榜。 “信息的DNA”正在迅速取代原子而成为人类生活中的基本交换物。尼葛洛庞帝向我们展示出这一变化的巨大影响。电视机与计算机屏幕的差别变得只是大小不同而已。从前所说的“大众”传媒正演变成个人化的双向交流。信息不再被“推给”消费者,相反,人们或他......一起来看看 《数字化生存》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具