java数据结构与算法刷题-----LeetCode450. 删除二叉搜索树中的节点

java数据结构与算法刷题目录(剑指Offer、LeetCode、ACM)-----主目录-----持续更新(进不去说明我没写完):https://blog.csdn.net/grd_java/article/details/123063846

文章目录

在这里插入图片描述

解题思路
  1. 如果当前结点node是要删除的结点,我们可以选择以下两种方案任意一种
  1. 右子树成为当前新的node。并将左子树插入右子树中
  2. 左子树成为新的node,并将右子树插入左子树中。
  1. 我们这里选择,提上来右子树,然后按照BST的规则,将左子树插入到提上来的右子树中

1. 递归

代码:时间复杂度O(n),空间复杂度O(n)

在这里插入图片描述

class Solution {
    public TreeNode deleteNode(TreeNode root,int key){
        if(root == null) return null;
        if(key < root.val){
            root.left = deleteNode(root.left,key);
            return root;//在左边,就没必要向下继续执行了
        }else if(key > root.val){
            root.right = deleteNode(root.right,key);
            return root;//在右边,就没必要向下继续执行了
        }
        if(key == root.val) return insertBST(root.right,root.left);
        return root;
    }
    //将node插入到以root为根结点的BST中,返回插入后的根结点
    public TreeNode insertBST(TreeNode root,TreeNode node){
        // if(root== null && node == null) return null;
        if(root == null) return node;//如果root为空,直接返回node
        if(node == null) return root;//如果node为空,直接返回root
        if(root.val < node.val){//如果node大,往右边插
            if(root.right == null) root.right = node;
            else insertBST(root.right,node);
        }else if(root.val > node.val){//node小,往左边插
            if(root.left == null) root.left = node;
            else insertBST(root.left,node);
        }
        return root;//返回插入后根结点。
    }

}
/**
 * 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;
 *     }
 * }
 */

2. 迭代

代码:时间复杂度O(n),空间复杂度O(1)

在这里插入图片描述

class Solution {
    public TreeNode deleteNode(TreeNode root,int key){
        if(root == null) return null;
        TreeNode cur = root,curParent = null;//cur表示当前迭代结点,curParent是cur的父结点
        //用cur找到要删除的结点
        while(cur != null && cur.val!=key){
            curParent = cur;
            if(key < cur.val) cur = cur.left;
            else if(key > cur.val) cur = cur.right;
        }
        if(cur == null) return root;//如果cur没找着,说明没有要删的对象,直接返回
        cur = insertBST(cur.right,cur.left);//否则将其左子树插入右子树中,再保存回cur变量中,cur将成为取代要删除结点的新结点
        if(curParent == null)return cur;//如果cur没有父结点,说明删除的是根结点,直接返回cur即可
        //如果cur的父结点不为null,说明删除的不是根
        if(curParent.left!=null&&curParent.left.val == key) curParent.left = cur;//如果删除的是父结点的左子树,就取代左子树
        else if(curParent.right != null && curParent.right.val == key) curParent.right = cur;//如果是右子树,就取代右子树
        return root;//返回根结点
    }
    //将node插入到以root为根结点的BST中,返回插入后的根结点
    public TreeNode insertBST(TreeNode root,TreeNode node){
        if(root == null && node == null) return null;
        else if(root == null) return node;
        else if(node == null) return root;
        TreeNode cur = root;
        while(cur != null){
            if(node.val < cur.val) {
                if(cur.left == null) {
                        cur.left = node;break;
                }
                else cur = cur.left;
            }else if(node.val > cur.val){
                if(cur.right == null) {cur.right = node;break;}
                else cur = cur.right;
            }
        }
        return root;
    }

}
  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

殷丿grd_志鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值