【LeetCode+JavaGuide打卡】Day22|235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点

学习目标:

  • 235. 二叉搜索树的最近公共祖先
  • 701.二叉搜索树中的插入操作
  • 450.删除二叉搜索树中的节点

学习内容:

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

题目链接&&文章讲解

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

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

java 复制代码
//递归法
//从上向下去递归遍历,第一次遇到 cur节点是数值在[q, p]区间中,那么cur就是 q和p的最近公共祖先。
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;
    }
}


//迭代法
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        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;
    }
}

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

题目链接&&文章讲解

java 复制代码
//在叶子节点找到插入位置
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        //终止条件
        if(root == null) {
            TreeNode node = new TreeNode(val);
            return node;
        }
        //左
        if(val < root.val){
            root.left = insertIntoBST(root.left, val);
        }
        //右
        if(val > root.val){
            root.right = insertIntoBST(root.right, val);
        }
        return root;
    }
}

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

题目链接&&文章讲解

有以下五种情况:

  • 第一种情况:没找到删除的节点,遍历到空节点直接返回了找到删除的节点
  • 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
  • 第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
  • 第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
  • 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点。
java 复制代码
class Solution {
    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 && root.right ==null) return root.left;
            else if(root.left == null && root.right !=null) return root.right;
            else  {
                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);
        }

        if(key > root.val){
            root.right = deleteNode(root.right, key);
        }
        return root;
    }
}

相关推荐
Tisfy5 分钟前
LeetCode 3637.三段式数组 I:一次遍历(三种实现)
算法·leetcode·题解·模拟·数组·遍历·moines
遨游xyz12 分钟前
数据结构-哈希表
算法·哈希算法
dyyx11130 分钟前
C++中的过滤器模式
开发语言·c++·算法
lrh1228001 小时前
详解决策树算法:分类任务核心原理、形成流程与剪枝优化
算法·决策树·机器学习
期末考复习中,蓝桥杯都没时间学了1 小时前
力扣刷题15
算法·leetcode·职场和发展
2301_817497331 小时前
C++中的装饰器模式高级应用
开发语言·c++·算法
m0_549416661 小时前
C++编译期字符串处理
开发语言·c++·算法
m0_581124191 小时前
C++中的适配器模式实战
开发语言·c++·算法
A尘埃1 小时前
零售连锁店生鲜品类销量预测——线性回归(Linear Regression)
算法·线性回归·零售
u0109272711 小时前
C++与人工智能框架
开发语言·c++·算法