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


    学习目标:

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

    学习内容:

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

    题目链接&&文章讲解
    给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先

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

    //递归法
    //从上向下去递归遍历,第一次遇到 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

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

    题目链接&&文章讲解

    //在叶子节点找到插入位置
    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

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

    题目链接&&文章讲解

    有以下五种情况:

    • 第一种情况:没找到删除的节点,遍历到空节点直接返回了找到删除的节点
    • 第二种情况:左右孩子都为空(叶子节点),直接删除节点, 返回NULL为根节点
    • 第三种情况:删除节点的左孩子为空,右孩子不为空,删除节点,右孩子补位,返回右孩子为根节点
    • 第四种情况:删除节点的右孩子为空,左孩子不为空,删除节点,左孩子补位,返回左孩子为根节点
    • 第五种情况:左右孩子节点都不为空,则将删除节点的左子树头结点(左孩子)放到删除节点的右子树的最左面节点的左孩子上,返回删除节点右孩子为新的根节点。
    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

  • 相关阅读:
    JVM垃圾回收总结
    C#-特性
    android debug 签名文件的别名和秘钥是什么???
    SpringBoot实现多数据源(三)【AOP + 自定义注解】
    【python】网络爬虫与信息提取--scrapy爬虫框架介绍
    跟着 GPT-4 从0到1学习 Golang 并发机制(三)
    AWS的RDS数据库开启慢查询日志
    【WSN】无线传感器网络 X-Y 坐标到图形视图和位字符串前缀嵌入方法研究(Matlab代码实现)
    Zookeeper安装
    ESP8266 如何使用 GPIO13 & GPIO15 进行 UART0 通信?
  • 原文地址:https://blog.csdn.net/qq_56832705/article/details/136167546