自学内容网 自学内容网

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

}

原文地址:https://blog.csdn.net/grd_java/article/details/136306114

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!