算法学习day22

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

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

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

例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]

img

示例 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 为不同节点且均存在于给定的二叉搜索树中。

示例 1:

img

输入:root = [4,2,6,1,3]
输出:1

示例 2:

img

输入:root = [1,0,48,null,null,12,49]
输出:1
迭代
  • 二叉搜索树确定了搜索的方向
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null) return null;
        TreeNode cur = root ;
        while(cur != null){
            if(cur.val > p.val && cur.val > q.val) {
                //搜索左子树
                cur = cur.left;
            }else if(cur.val < p.val && cur.val < q.val) {
                //搜索右子树
                cur = cur.right;
            }else{
                return cur;
            }
        }
        return null;
    }
}
递归
  • 根据搜索值得大小确定搜索的方向
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //终止条件
        if(root == null) return null;
        //单层遍历逻辑
        if(root.val > p.val && root.val > q.val){
            //向左搜索
            TreeNode left = lowestCommonAncestor(root.left , p , q );
            if(left != null) return left;
        }
        if(root.val < p.val && root.val<q.val){
            //向右搜索
            TreeNode right = lowestCommonAncestor(root.right , p ,q);
            if(right != null) return right;
        }
        return root;
    }
}

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

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

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

示例 1:

img

输入:root = [4,2,7,1,3], val = 5
输出:[4,2,7,1,3,5]
解释:另一个满足题目要求可以通过的树是:

示例 2:

输入:root = [40,20,60,10,30,50,70], val = 25
输出:[40,20,60,10,30,50,70,null,null,25]

示例 3:

输入:root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
输出:[4,2,7,1,3,5]

提示:

  • 树中的节点数将在 [0, 104]的范围内。
  • -108 <= Node.val <= 108
  • 所有值 Node.val独一无二 的。
  • -108 <= val <= 108
  • 保证 val 在原始BST中不存在
迭代
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root == null) return new TreeNode(val);
        //迭代
        TreeNode pre = null ,cur = root ;
        while(cur != null) {
            pre = cur ;
            if(cur.val > val){
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }
        //循环结束后,pre是待插入节点的父节点
        if(pre.val > val){
            pre.left = new TreeNode(val);
        }else{
            pre.right = new TreeNode(val);
        }
        return root ;
    }
}
递归
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        // 如果根节点为空,则将该节点作为根节点插入
        if(root == null) {
            return new TreeNode(val);
        }

        // 如果要插入的节点的值小于当前节点的值,则将该节点插入当前节点的左子树中
        if(val < root.val) {
            root.left = insertIntoBST(root.left, val);
        }
        // 如果要插入的节点的值大于当前节点的值,则将该节点插入当前节点的右子树中
        else if(val > root.val) {
            root.right = insertIntoBST(root.right, val);
        }

        return root;
    }

}

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

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

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

  1. 首先找到需要删除的节点;
  2. 如果找到了,删除它。

示例 1:

img

输入:root = [5,3,6,2,4,null,7], key = 3
输出:[5,4,6,2,null,null,7]
解释:给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。
一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。
另一个正确答案是 [5,2,6,null,4,null,7]。

示例 2:

输入: root = [5,3,6,2,4,null,7], key = 0
输出: [5,3,6,2,4,null,7]
解释: 二叉树不包含值为 0 的节点

示例 3:

输入: root = [], key = 0
输出: []

提示:

  • 节点数的范围 [0, 104].
  • -105 <= Node.val <= 105
  • 节点值唯一
  • root 是合法的二叉搜索树
  • -105 <= key <= 105
递归
  • 找到删除节点时,判断终止条件
    • 当前节点为空,返回空
    • 当前节点不为空,根据待删除节点的左右子节点是否为空,分四种情况
      • 左右子节点都不为空时,将左子节点移动到右子节点的最小左子节点上即可
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        //终止条件1
        if(root == null) {
            return null;
        }
        //终止条件2,分四种情况
        if(root.val == key){
            //1.左右子树都为空
            if (root.left == null && root.right == null) {
                return null;
            }
            //2.左子树不为空,右子树为空
            if(root.left != null && root.right == null){
                return root.left ;
            }
            //3.左子树为空,右子树不为空
            if (root.right != null && root.left == null) {
                return root.right ;
            }
            //4.左右子树都存在
            if(root.left != null && root.right != null){
                //此处将左子树移动到右子树的“最左子树”上,保证二叉搜索树的特征,
                //找到删除节点的右子树的最小左子树
                TreeNode cur = root.right;
                while(cur.left != null){
                    //找到最左叶子节点
                    cur = cur.left;
                }
                cur.left = root.left;
                return root.right ;
            }

        }
        if(key < root.val){
            root.left = deleteNode(root.left,key);
        }else{
            root.right = deleteNode(root.right,key);
        }
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值