代码随想录算法刷题训练营day22

代码随想录算法刷题训练营day22:LeetCode(236)二叉树的最近公共祖先、LeetCode(235) 二叉搜索树的最近公共祖先、LeetCode(701)二叉搜索树中的插入操作、LeetCode(450)删除二叉搜索树中的节点

LeetCode(236)二叉树的最近公共祖先
题目

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //判断终止----------采用后续遍历方法----从下往上遍历
        //1、根为空
        if(root==null){
            return null;
        }
        //2、判断左右子树的情况-----递归思想----返回null---即为没有
        if(root.val==p.val||root.val==q.val){
            return root;//返回当前的pq值---------上面判断的是返回值
        }
        //递归遍历----左右根
        //左右子树的处理逻辑----查看左右子树是否出现pq值----左右子树为空--的那种情况
        TreeNode leftTreeNode=lowestCommonAncestor(root.left, p, q);
        TreeNode rightTreeNode=lowestCommonAncestor(root.right, p, q);
        //处理根节点-----重点------下面是判断的是有没有
        if(leftTreeNode!=null&&rightTreeNode!=null){
            return root;//说明左右子树均找到p或者q的值
        }else if(leftTreeNode==null&&rightTreeNode!=null){
            return rightTreeNode;//有确定的了就不会发生变化了
        }else if(leftTreeNode!=null&&rightTreeNode==null){
            return leftTreeNode;
        }else{
            return null;
        }
    }
}

LeetCode(235) 二叉搜索树的最近公共祖先
题目

代码

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //判断终止条件
        //终止条件1、若根节点为空的时候
        if(root==null){
            return root;
        }
        //终止条件2、若根节点为pq的某个值时----此时在遍历的时候就已经进行测试了
        if(root.val==p.val||root.val==q.val){
            return root;
        }
        //递归遍历
        //先左子树遍历---发现左子树是否包含p或者q值
        TreeNode leftTreeNode=lowestCommonAncestor(root.left, p, q);
        //同样遍历右子树
        TreeNode rightTreeNode=lowestCommonAncestor(root.right, p, q);
        //处理根节点
        if(leftTreeNode!=null&&rightTreeNode!=null){
            return root;
        }else if(leftTreeNode!=null&&rightTreeNode==null){
            return leftTreeNode;
        }else if(leftTreeNode==null&&rightTreeNode!=null){
            return rightTreeNode;
        }else{
            return null;//对应前面考虑到的左右子树均为空的情况----静心判断
        }

    }
    }

LeetCode(701)二叉搜索树中的插入操作
题目

代码

java 复制代码
/**
 * 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;
 *     }
 * }
 */
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        //在二叉搜索树上插入节点,因为仅在叶子节点上进行插入就能满足所有的插入情况---所以不需要改变树的结构
        //通过中序遍历+递归算法去做
        if(root==null){
            //此时为构造新节点占的位置
            TreeNode newNode=new TreeNode(val);
            return newNode;//把新子树创建出来,并返回回去
        }
        //进行判断,判断朝左右子树递归的方向
        if(root.val>val){
            root.left=insertIntoBST(root.left, val);//将判断后的值连上
        }
        if(root.val<val){
            root.right=insertIntoBST(root.right, val);//将判断后的右边的值返回回去---左右两边仅有一边的值进行返回
        }
        //连接完成之后,将根节点的值进行返回
        return root;//重点----仔细理解
    }
}

LeetCode(450)删除二叉搜索树中的节点
题目

代码

java 复制代码
/**
 * 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;
 *     }
 * }
 */
class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        //考虑删除节点情况-----一共分为五种情况
        /* 
         * 1、没有找到删除节点
         * 2、删除节点为叶子节点
         * 3、删除节点有左子树,没有右子树
         * 4、删除节点有右子树,没有左子树
         * 5、删除节点既有左子树又有右子树
         */
        //递归算法+先序遍历----先处理根节点在处理左子树和右子树
        //递归的终止条件
        if(root==null){
            return null;//正常的终止条件------同时也是找不到删除节点的情况
        }
        //第二个终止条件----即找到要删除的元素
        if(root.val==key){
            //判断四种删除节点的情况
            //1、是叶子节点
            if(root.left==null&&root.right==null){
                return null;//返回为空,后面连接对应左子树或者右子树
            }else if(root.left!=null&&root.right==null){
                return root.left;//2、删除节点有左子树,没有右子树
            }else if(root.left==null&&root.right!=null){
                return root.right;//3、删除节点有右子树。没有左子树
            }else{
                //最复杂的一种情况---删除根节点的左右子树均不为空
                TreeNode currentNode=root.right;
                //找到右子树最左边的节点
                while (currentNode.left!=null) {
                  currentNode=currentNode.left;
                }
                //将根节点的左子树拿出来
                currentNode.left=root.left;
                //此时又变成左空由不空的情况
                return root.right;
            }
        }
            //终止条件处理完了,现在开始处理单次递归的情况
            if(root.val>key){
                //向左边遍历
                root.left=deleteNode(root.left, key);//此时返回的值直接连接到当前的根节点上
            }
            if(root.val<key){
                //向右边遍历
                root.right=deleteNode(root.right, key);
            }
            return root;//最后将根节点返回//最后将根节点返回
    }

    }
相关推荐
爬山算法几秒前
Maven(6)如何使用Maven进行项目构建?
java·maven
.生产的驴4 分钟前
Electron Vue框架环境搭建 Vue3环境搭建
java·前端·vue.js·spring boot·后端·electron·ecmascript
wjs20248 分钟前
Chrome 浏览器:现代网络浏览的先锋
开发语言
爱学的小涛12 分钟前
【NIO基础】基于 NIO 中的组件实现对文件的操作(文件编程),FileChannel 详解
java·开发语言·笔记·后端·nio
吹老师个人app编程教学13 分钟前
详解Java中的BIO、NIO、AIO
java·开发语言·nio
爱学的小涛13 分钟前
【NIO基础】NIO(非阻塞 I/O)和 IO(传统 I/O)的区别,以及 NIO 的三大组件详解
java·开发语言·笔记·后端·nio
北极无雪17 分钟前
Spring源码学习:SpringMVC(4)DispatcherServlet请求入口分析
java·开发语言·后端·学习·spring
琴智冰21 分钟前
SpringBoot
java·数据库·spring boot
binqian21 分钟前
【SpringSecurity】基本流程
java·spring
Mopes__33 分钟前
Python | Leetcode Python题解之第452题用最少数量的箭引爆气球
python·leetcode·题解