力扣爆刷第90天之hot100五连刷36-40

力扣爆刷第90天之hot100五连刷36-40

一、94. 二叉树的中序遍历

题目链接:https://leetcode.cn/problems/binary-tree-inorder-traversal/description/?envType=study-plan-v2&envId=top-100-liked
思路:最简单的中序遍历,无需多说。


class Solution {
    List<Integer> list = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        order(root);
        return list;
    }
    void order(TreeNode root) {
        if(root == null) {
            return ;
        }
        order(root.left);
        list.add(root.val);
        order(root.right);
    }
}

二、104. 二叉树的最大深度

题目链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/?envType=study-plan-v2&envId=top-100-liked
思路:后续遍历到根节点,选取左右子树的结果中的最大值作为子树深度,然后加1即本节点,返回。

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left, right) + 1;
    }
}

三、26. 翻转二叉树

题目链接:https://leetcode.cn/problems/invert-binary-tree/description/?envType=study-plan-v2&envId=top-100-liked
思路:前序遍历,然后逐个交换节点左右子树即可。


class Solution {
    public TreeNode invertTree(TreeNode root) {
        order(root);
        return root;
    }
    void order(TreeNode root) {
        if(root == null) return ;
        TreeNode t = root.left;
        root.left = root.right;
        root.right = t;
        order(root.left);
        order(root.right);
    }

}

四、101. 对称二叉树

题目链接:https://leetcode.cn/problems/symmetric-tree/description/?envType=study-plan-v2&envId=top-100-liked
思路:判断是否是对称二叉树,直接把根节点的左右子树,作为一颗树进行遍历。

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return isEquels(root.left, root.right);
    }
    boolean isEquels(TreeNode child1, TreeNode child2) {
        if(child1 == null && child2 == null) return true;
        if((child1 == null && child2 != null) || (child1 != null && child2 == null)) return false;
        if(child1.val != child2.val) return false;
        return isEquels(child1.left, child2.right) && isEquels(child1.right, child2.left);
    }
}

五、543. 二叉树的直径

题目链接:https://leetcode.cn/problems/diameter-of-binary-tree/description/?envType=study-plan-v2&envId=top-100-liked
思路:其实求的就是任意一个节点其左右子树能够连接的最长边长,那么问题就转化为求每个节点左子树的最长路径和右子树的最长路径和。

class Solution {
  
    int max = 0;
    public int diameterOfBinaryTree(TreeNode root) {
       order(root);
       return max;
    }
    
    int order(TreeNode root) {
        if(root == null) return 0;
        int left = order(root.left);
        int right = order(root.right);
        max = Math.max(max, left + right);
        return Math.max(left, right) + 1;
    }

    
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

当年拼却醉颜红

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

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

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

打赏作者

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

抵扣说明:

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

余额充值