代码随想录【Day22】| 235. 二叉搜索树的最近公共祖先、701. 二叉搜索树中的插入操作、450. 删除二叉搜索树中的节点

235. 二叉搜索树的最近公共祖先

题目链接

题目描述:
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
在这里插入图片描述
示例 1:

输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6。
示例 2:

输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
说明:

所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉搜索树中。

难点:

思路:
根据BST的性质,如果找到某个子树根节点落在给定两个节点的值的区间内,那么就返回根节点

时间复杂度:O()
空间复杂度:O()

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null || root == p || root == q) return root;

        if ((p.val < root.val && q.val > root.val) || (p.val > root.val && q.val < root.val)) {
            return root;
        }

        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);

        if (left != null && right != null) return root;
        if (left == null && right != null) return right;
        if (left != null && right == null) return left;

        return null;
    }
}

//代码随想录简化
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
        if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
        return root;
    }
}

时长:
20min

收获:
把握BST的性质


701. 二叉搜索树中的插入操作

题目链接

题目描述:
给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。

注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。
在这里插入图片描述
提示:

给定的树上的节点数介于 0 和 10^4 之间
每个节点都有一个唯一整数值,取值范围从 0 到 10^8
-10^8 <= val <= 10^8
新值和原始二叉搜索树中的任意节点值都不同

难点:

思路:
根据BST性质向左向右遍历,到达目标空位置进行节点创建

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return new TreeNode(val); //根节点为空

        insertBST(root, val);
        return root;
    }

    private void insertBST(TreeNode root, int val) {
        TreeNode cur = root;
        if (root.val > val) { //根节点值大于插入节点,向左
            if (root.left == null) {
                TreeNode newNode = new TreeNode(val);
                root.left = newNode;
                return;
            }
            insertBST(root.left, val);
        }else { //根节点值小于插入节点,向右
            if (root.right == null) {
                TreeNode newNode = new TreeNode(val);
                root.right = newNode;
                return;
            }
            insertBST(root.right, val);
        }
    }
}

//代码随想录简化
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
            return new TreeNode(val);
            
        if (root.val < val){
            root.right = insertIntoBST(root.right, val); // 递归创建右子树
        }else if (root.val > val){
            root.left = insertIntoBST(root.left, val); // 递归创建左子树
        }
        return root;
    }
}

迭代法

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return new TreeNode(val);
        TreeNode newRoot = root;
        TreeNode pre = root;
        while (root != null) {
            pre = root;
            if (root.val > val) {
                root = root.left;
            } else if (root.val < val) {
                root = root.right;
            } 
        }
        if (pre.val > val) {
            pre.left = new TreeNode(val);
        } else {
            pre.right = new TreeNode(val);
        }

        return newRoot;
    }
}

时长:
20min

收获:
合并条件

掌握递归法中的左右孩子传递进行操作


450. 删除二叉搜索树中的节点

题目链接

题目描述:
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。

一般来说,删除节点可分为两个步骤:

首先找到需要删除的节点; 如果找到了,删除它。 说明: 要求算法时间复杂度为 O ( h ) O(h) O(h),h 为树的高度。

示例:

在这里插入图片描述

难点:

思路:

时间复杂度:O()
空间复杂度:O()

没写出来。。。主要是根节点为目标节点,并且左右子树不为空的调整方法
写的很乱= =

TreeNode pre; //要记录上一个节点,否则删点会断链
    public TreeNode deleteNode(TreeNode root, int key) {
        if (root == null) return null;
        if (root.val == key) { //找到目标节点
            if (root.left == null && root.right == null) { //根节点左右为空
                return null;
            }else if (root.left == null) {
                return root.right;
            }else if (root.right == null) {
                return root.left;
            }

            if (pre == null) {
                //怎么处理?
            }

            if (pre != null && pre.val > key) {
                if (root.right != null) {
                    pre.left = root.right;
                }
                pre.left.left = root.left;
                return pre.left;
            }
            if (pre != null && pre.val < key) {
                if (root.right != null) {
                    pre.left = root.right;
                }
                pre.left.left = root.left;
                return pre.right;
            }
        }
        pre = root;
        if (key > root.val) {
            root.right = deleteNode(root.right, key);
        }
        if (key < root.val) {
            root.left = deleteNode(root.left, key);
        }
        return root;
    }

二叉树删除根节点的调整方法演示
正确解答:

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        return root = delete(root, key);
    }

    private TreeNode delete(TreeNode root, int key) {
        if (root == null) return null;

        if (root.val > key) {
            root.left = delete(root.left, key);
        }else if (root.val < key) {
            root.right = delete(root.right, key);
        }else {
            if (root.left == null) return root.right;
            if (root.right == null) return root.left;

            TreeNode cur = root.right;
            while (cur.left != null) {
                cur = cur.left;
            }
            cur.left = root.left;
            root = root.right;
        }
        return root;
    }
}

时长:
45min

收获:
BST删除根节点的调整方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值