6.23删除二叉搜索树中的节点(LC450-M)

算法:

一共有五种可能的情况:

  • 第一种情况:没找到删除的节点,遍历到空节点直接返回了
  • 找到删除的节点
    • 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
    • 第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
    • 第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
    • 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点。

第五种情况,比如要删除节点7,可以让它的左孩子或者右孩子去继位。这里是让左孩子去继位,左孩子比7小,右孩子比7大,那左孩子应该继位在右孩子的最小的节点的左边,即8左边。然后,让3指向9。

调试过程:

复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {        
        if (root == null) return root;
//1.找不到key节点,自动返回原root
        if (root.val == key) {
//2.左右都空,说明是叶子,直接删除
        if (root.left==null && root.right==null) return null;
//3.左空右不空,右上移
        if (root.left==null && root.right!=null) {
            root = root.right;
            return root;
        }
//4.右空左不空,左上移        
        if (root.left!=null && root.right==null) {
            root = root.left;
            return root;
        }
//5.左右都不空,root的左孩子移到右孩子的最左边
        if (root.left!=null && root.right!=null){
            TreeNode left = root.left;
            root = root.right;
            while (root.left!=null){
                root = root.left;
            }
            root.left = left;
            return root;
        }
        }
        if (root.val < key) deleteNode(root.left, key);  
        if (root.val > key) deleteNode(root.right, key);      
        return root;

    }
}

原因:

左右都不空时,代码有问题。

代码逻辑不对,没有中间变量cur,相当于少了个变量cur去实现交换操作。

而且,递归处没有赋值给root.left和root.right(因为这里的递归是有返回值TreeNode的)!!!!无法真正实现递归(这一点老是忘记)

修改后:

复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {        
        if (root == null) return root;
//1.找不到key节点,自动返回原root
        if (root.val == key) {
//2.左右都空,说明是叶子,直接删除
        if (root.left==null && root.right==null) return null;
//3.左空右不空,右上移
        if (root.left==null && root.right!=null) {
            root = root.right;
            return root;
        }
//4.右空左不空,左上移        
        if (root.left!=null && root.right==null) {
            root = root.left;
            return root;
        }
//5.左右都不空,root的左孩子移到右孩子的最左边
        if (root.left!=null && root.right!=null){
            TreeNode cur = root.right;
            while (cur.left!=null){
                cur = cur.left;
            }
            cur.left = root.left;
            root = root.right;
            return root;
        }
        }
        if (root.val < key) root.left=deleteNode(root.left, key);  
        if (root.val > key) root.right=deleteNode(root.right, key);      
        return root;

    }
}

原因:

递归处逻辑不对。

应该是key比root.val小时,向左搜索

应该是key比root.val大时,向右搜索

正确代码:

复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {        
        if (root == null) return root;
//1.找不到key节点,自动返回原root
        if (root.val == key) {
//2.左右都空,说明是叶子,直接删除
        if (root.left==null && root.right==null) return null;
//3.左空右不空,右上移
        if (root.left==null && root.right!=null) {
            root = root.right;
            return root;
        }
//4.右空左不空,左上移        
        if (root.left!=null && root.right==null) {
            root = root.left;
            return root;
        }
//5.左右都不空,root的左孩子移到右孩子的最左边
        if (root.left!=null && root.right!=null){
            TreeNode cur = root.right;
            while (cur.left!=null){
                cur = cur.left;
            }
            cur.left = root.left;
            root = root.right;
            return root;
        }
        }
        if (key< root.val) root.left=deleteNode(root.left, key);  
        if (key> root.val) root.right=deleteNode(root.right, key);      
        return root;

    }
}

时间空间复杂度:

时间复杂度:

O(n),其中 n为 root的节点个数。最差情况下,寻找和删除 cur各需要遍历一次树。

空间复杂度:

O(n),其中 n为 root的节点个数。递归的深度最深为 O(n)。

相关推荐
饮水桶_T19 小时前
JAVA IO流笔记
开发语言·数据结构·算法
wo_jack1 天前
C++ 入门笔记(十七):list 入门 —— 为什么 list 不能 sort?
数据结构·c++·面试·stl·list
zaizai10071 天前
算法(左程云)1
数据结构
charliejohn1 天前
计算机考研 408 数据结构 图论-关键路径
数据结构·图论
David猪大卫2 天前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
vvilkim2 天前
数据结构类型、区别与应用场景:一篇讲透选型逻辑
数据结构
阿卡好可爱2 天前
Lua 热更新不生效:改完文件线上仍是旧逻辑,package.loaded 与 upvalue 重绑定详解
数据结构·分布式·后端·架构·lua
心仪久了会沦陷3 天前
C语言数据结构入门——树与二叉树详解:概念、堆实现与链式遍历
c语言·数据结构·算法
码少女3 天前
数据结构——图的基础
数据结构