【JAVA刷题初阶】刷爆力扣第十弹——二叉树


🎪 前言:关于JAVA刷题

🤙关于JAVA的学习出了看视频以外,那就是刷题了,朋友们,你们有没有过这样的感觉,在网上看了视频过后感觉自己什么都听懂了,但就是写题和做项目时无从下手,或者就是因为某个细节一直错一直改,那背后的原因是什么呢?四个字——题刷少了,这里新一建议去Leetcode看看,那里的题库资源很丰富,并且在全球都有广泛涉猎。不仅如此,这里还有 课程 + 刷题 + 面经 + 求职 + 讨论区分享解题思路,用过的人都说好😜
在这里插入图片描述
👉除此之外,我的建议是初学者从简单题开始练习,因为简单题是一切题的基础,一切的困难题都是从简单题衍生而来的,每天刷那么2~3题,后期再慢慢刷中等题,困难题,经过一段时间后会有很不错的提升
在这里插入图片描述
此外,在我们有一定的提升之后,我们便可以去刷剑指offer了,在这里预祝各位大学生以及初学者都拿到自己满意的offer!💖


第一题:二叉树最近的公共祖先

👇👇👇
做题链接戳这里:236.二叉树最近的公共祖先

🚀 题目描述

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

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

🚀示例

在这里插入图片描述

🚀提示

● 树中节点数目在范围 [2, 105] 内。
● -109 <= Node.val <= 109
● 所有 Node.val 互不相同 。
● p != q
● p 和 q 均存在于给定的二叉树中。

🧰题解

二叉搜索树思路

这道题曾经是字节的一道面试题,我们从两个思路来出发,一是二叉搜索树:

在这里插入图片描述

我们先考虑如果它是一颗二叉搜索树,那么我们需要考虑的情况有以下几种:(1)根节点为空,则返回空 (2) 根节点的值是否等于这两个节点,如果是,则返回根节点(3) 我们借助二叉搜索树的特性,比较它们与根节点大小,从而决定递归地返回哪个节点

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null){
            return null;
        }

        if (root == p || root == q){
            return root;
        }
        /*if (p.val < root.val && q.val < root.val){
            return lowestCommonAncestor(root.left, p, q);
        }
        if (p.val > root.val && q.val > root.val){
            return lowestCommonAncestor(root.right, p, q);
        }
        if (p.val > root.val && q.val < root.val){
            return root;
        }*/
    }

好了,现在我们推广到一般二叉树情况,前两步肯定是一样的,现在相比二叉搜索树我们就不是比较它value域的大小了,就是比较它是否为空了,我们先递归的访问两颗子树,再将其返回值存起来,如果两个返回值都不为空,说明两节点在左右两棵大子树中,则根节点为其公共祖先,如果其中有一方不为空,另一方为空,则返回不为空的一方即为祖先。

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

        if (root == p || root == q){
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);

        if (leftTree != null && rightTree != null){
            return root;
        }else if (leftTree != null){
            return leftTree;
        }else{
            return rightTree;
        }
    }
}

在这里插入图片描述

双栈存储法

要是面试官还问,还有没有其它解法呢?别慌,当然有,我们来仔细康康这个用例,瞪大眼睛看了昂:
在这里插入图片描述

这个是不是似曾相识的感觉呢?跟我们的那啥链表求交点是不是挺相似啊😀,那我们思路倒是有了,那我们怎么写代码呢,两个链表求交点,两条路径,存储两条路径,啧啧,对!就是栈,我们用两个栈来存储路径,最后在通过弹出来确定相同的公共祖先:

public boolean getRows(TreeNode root, TreeNode p, Stack<TreeNode> stack){
        if (root == null) return false;
        stack.push(root);
        
        if (root == p){
            return true;
        }
        boolean flg = getRows(root.left, p,stack);
        if (flg){
            return true;
        }
        
        flg = getRows(root.right, p, stack);
        if (flg){
            return true;
        }
        
        stack.pop();
        return false;
    }
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;

        Stack<TreeNode> stack1 = new Stack<>();
        getRows(root, p, stack1);
        Stack<TreeNode> stack2 = new Stack<>();
        getRows(root, q, stack2);

        int size1 = stack1.size();
        int size2 = stack2.size();
        if (size1 > size2){
            int size = size1 - size2;
            while (size > 0){
                stack1.pop();
                size--;
            }
        }else{
            int size = size2 - size1;
            while (size > 0){
                stack2.pop();
                size--;
            }
        }

        while (!stack1.empty() && !stack2.empty()){
            if (stack1.peek() == stack2.peek()){
                return stack1.pop();
            }else{
                stack1.pop();
                stack2.pop();
            }
        }
        return null;
    }

在这里插入图片描述

第二题:从前序与中序遍历序列构造二叉树

👇👇👇
做题链接戳这里,105.从前序与中序遍历序列构造二叉树

🚀题目描述

给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

🚀示例

在这里插入图片描述

🚀提示

● 1 <= preorder.length <= 3000
● inorder.length == preorder.length
● -3000 <= preorder[i], inorder[i] <= 3000
● preorder 和 inorder 均 无重复 元素
● inorder 均出现在 preorder
● preorder 保证 为二叉树的前序遍历序列
● inorder 保证 为二叉树的中序遍历序列

🧰题解

我们之前博文讲过关于知道前序遍历和中序遍历求二叉树的方法,这道题就是将其转换成了代码而已,先根据前序遍历找到根节点,然后根据找到根节点在中序遍历中的位置,该位置的左边,即为该二叉树的左子树,右边即为该二叉树的右子树,好了既然到这儿,就应该是递归了。

class Solution {
    public int preIndex = 0;
    public TreeNode createTreePandI(int[] preprder, int[] inorder, int inbegin, int inend){
        if (inbegin > inend){
            return null;
        }

        TreeNode root = new TreeNode(preprder[preIndex]);

        int rootindex = findRootindex(inorder,inbegin,inend,preprder[preIndex]);
        if (rootindex == -1){
            return null;
        }
        preIndex++;
        root.left = createTreePandI(preprder,inorder,inbegin,rootindex - 1);
        root.right = createTreePandI(preprder,inorder,rootindex + 1, inend);

        return root;
    }
    public int findRootindex(int[] inorder, int inbegin, int inend, int key){
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == key){
                return i;
            }
        }
        return -1;
    }
    //105.从前序与中序遍历序列构造二叉树
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder == null || inorder == null) return null;

        return createTreePandI(preorder,inorder,0, inorder.length - 1);
    }
}

在这里插入图片描述

第三题:从中序与后续遍历序列构造二叉树

👇👇👇
做题链接戳这里:106.从中序与后续遍历序列构造二叉树

🚀 题目描述

给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

🚀示例

在这里插入图片描述

🚀提示

● 1 <= inorder.length <= 3000
● postorder.length == inorder.length
● -3000 <= inorder[i], postorder[i] <= 3000
● inorder 和 postorder 都由 不同 的值组成
● postorder 中每一个值都在 inorder 中
● inorder 保证是树的中序遍历
● postorder 保证是树的后序遍历

🧰题解

这跟我们上道题不能说一模一样,只能说完全相同,只需要改一下参数与顺序即可。

public int postIndex = 0;//回溯过程会改变其值
    public TreeNode createTreeByPandI(int[] inorder, int[] postorder,int inbegin, int inend){
        if (inbegin > inend){
            return null;
        }
        TreeNode root = new TreeNode(postorder[postIndex]);
        int rootIndex = findIndexOfI(inorder,inbegin,inend,postorder[postIndex]);
        if (rootIndex == -1){
            return null;
        }
        postIndex--;
        root.right = createTreeByPandI(inorder, postorder,rootIndex + 1,inend);
        root.left = createTreeByPandI(inorder,postorder, inbegin, rootIndex - 1);
        return root;
    }
    private int findIndexOfI(int[] inorder,int inbegin, int inend, int key){
        for (int i = inbegin; i <= inend; i++) {
            if (inorder[i] == key){
                return i;
            }
        }
        return -1;
    }

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if (postorder == null || inorder == null) return  null;
        postIndex = postorder.length - 1;

        return createTreeByPandI(inorder,postorder,0,inorder.length - 1);
    }

在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Corwttaml

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值