• 力扣labuladong——一刷day05


    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


    前言


    一、力扣104. 二叉树的最大深度

    遍历

    /**
     * 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 {
        int depth = 0,res = 0;
        public int maxDepth(TreeNode root) {
            traverser(root);
            return depth;
        }
        public void traverser(TreeNode root){
            if(root == null){
                return ;
            }
            res ++;
            depth = Math.max(depth,res);
            traverser(root.left);
            traverser(root.right);
            res --;
        }
    }
    
    • 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

    分解

    /**
     * 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 int maxDepth(TreeNode root) {
            return depth(root);
        }
        public int depth(TreeNode root){
            if(root == null){
                return 0;
            }
            int l = depth(root.left);
            int r = depth(root.right);
            return l > r ? l + 1 : r + 1;
        }
    }
    
    • 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

    二、力扣543. 二叉树的直径

    /**
     * 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 {
        int path = 0;
        public int diameterOfBinaryTree(TreeNode root) {
            fun(root);
            return path-1;
        }
        public int fun(TreeNode root){
            if(root == null){
                return 0;
            }
            int l = fun(root.left);
            int r = fun(root.right);
            path = Math.max(path, l + r + 1);
            return l > r ? l + 1:r + 1;
        }
    }
    
    • 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

    三、力扣144. 二叉树的前序遍历

    分解的思路

    /**
     * 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 List<Integer> preorderTraversal(TreeNode root) {
            return fun(root);
        }
        public List<Integer> fun(TreeNode root){
            List<Integer> arr = new LinkedList<>();
            if(root == null){
                return arr;
            }
            arr.add(root.val);
            arr.addAll(fun(root.left));
            arr.addAll(fun(root.right));
            return arr;
        }
    }
    
    • 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
  • 相关阅读:
    YOLOv4: Optimal Speed and Accuracy of Object Detection(2020.4)
    minio使用案例(Springboot)
    (iView)表格JSON字符串转为键值对,去掉对象的双引号和花括号,省略号隐藏,悬浮显示完整内容
    Qt 视口和窗口的区别
    验收测试的内容和流程有哪些?
    AI全栈大模型工程师(五)Prompt 的构成
    《软件工程导论(第六版)》 张海藩 复习笔记
    看不懂懂车大爆炸,你就错过了国产小车的王炸!
    数据结构-图-基础知识
    lv6 嵌入式开发-4 gdb调试多进程程序
  • 原文地址:https://blog.csdn.net/ResNet156/article/details/133984938