二叉树相关

二叉树相关

  • [力扣104 二叉树最大深度 普通递归遍历](#力扣104 二叉树最大深度 普通递归遍历)
  • [力扣 104 递归遍历2](#力扣 104 递归遍历2)
  • 二叉树求前序遍历结果
  • [二叉树求 每个节点所在层数与每个节点的左右子树上的节点总数](#二叉树求 每个节点所在层数与每个节点的左右子树上的节点总数)
  • [力扣 543 二叉树的直径](#力扣 543 二叉树的直径)

力扣104 二叉树最大深度 普通递归遍历

java 复制代码
int depth = 0;
    int maxDepth = 0;
    public int maxDepth(TreeNode root) {
        traverse(root);
        return maxDepth;
    }

    public void traverse(TreeNode root){
        if(root == null){
            return;
        }

        depth++;
        if(root.right == null && root.left == null){
            maxDepth = maxDepth>depth?maxDepth:depth;
            // 到达叶子节点 记录最大深度 
            depth--;
            return;
            // 向上回溯 depth--
        }

        traverse(root.left);
        traverse(root.right);
        // 左右都执行完 执行到这一步说明
        // root.left 与 root.right 至少有一个不为空
        // root 自己已经在depth中++过了 
        // 向上回溯 depth--
        //(由于最底层的也会在访问root时(root不为空)做depth++ 
        // 在return 的时候做depth--)
        // 所以回溯到当前递归函数 depth 合法
        depth--;
        return;
    }

力扣 104 递归遍历2

java 复制代码
int depth = 0;
    int maxDepth = 0;
    public int maxDepth(TreeNode root) {
        maxDepth = traverse(root);
        return maxDepth;
    }

    public int traverse(TreeNode root){
        if(root == null){
            return 0;
        }
        
        int left = traverse(root.left);
        int right = traverse(root.right);

        maxDepth = left>=right?left:right;
        maxDepth++;
        // 访问root,高度+1

        return maxDepth;
    }

二叉树求前序遍历结果

java 复制代码
/*前序遍历  递归方法1 */
    public static void PreOrderTraverse(TreeNode root) {
        if (root != null) {
            linkedListPreOrder.add(root.val);
            PreOrderTraverse(root.left);
            PreOrderTraverse(root.right);
        } else {
            return;
        }
    }

二叉树求 每个节点所在层数与每个节点的左右子树上的节点总数

java 复制代码
/*如何打印出每个节点的左右子树各有多少节点*/
    public static int traverseRootCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftCount = traverseRootCount(root.left);
        int rightCount = traverseRootCount(root.right);
        System.out.printf("当前节点为%d, 它的左子树有%d个节点, 右子树有%d个节点\n", root.val, leftCount, rightCount);
        return leftCount + rightCount + 1;
    }

    /*如何打印出每个节点的左右子树各有多少节点  与所在层次 */
    public static int traverseRootCount(TreeNode root, int level) {
        if (root == null) {
            return 0;
        }
        level++;
        int leftCount = traverseRootCount(root.left, level);
        int rightCount = traverseRootCount(root.right, level);
        System.out.printf("当前节点为%d, 位于第%d层, 它的左子树有%d个节点, 右子树有%d个节点\n",
                root.val, level, leftCount, rightCount);

        return leftCount + rightCount + 1;//返回节点个数
    }

力扣 543 二叉树的直径

java 复制代码
int max = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        // 二叉树最长直径 即 二叉树 左右子树最长深度之和
        // 对所有节点都求一次直径!!!!
        if(root==null){
            return 0;
        }
        int leftMax = depthMax(root.left);
        int rightMax = depthMax(root.right);

        max = Math.max(max,leftMax+rightMax);
        return max;
    }

    public int depthMax(TreeNode root){
        if(root == null){
            return 0;
        }
        
        int left  = depthMax(root.left) + 1;
        int right = depthMax(root.right) + 1;

        max = Math.max(max,left-1+right-1);
        // 更新最大直径的值 直到最后一次递归最外层函数时 计算 以最初的root为根的最大直径
        return Math.max(left,right);
    }
相关推荐
醉殇姒若梦遗年19 分钟前
怎么用idea打jar包
java·intellij-idea·jar
林九生23 分钟前
【Docker】Docker环境下快速部署Ollama与Open-WebUI:详细指南
java·docker·eureka
Aric_Jones1 小时前
lua入门语法,包含安装,注释,变量,循环等
java·开发语言·git·elasticsearch·junit·lua
Akiiiira1 小时前
【日撸 Java 三百行】Day 12(顺序表(二))
java·开发语言
EndingCoder1 小时前
2025年JavaScript性能优化全攻略
开发语言·javascript·性能优化
Chase_Mos5 小时前
Spring 必会之微服务篇(1)
java·spring·微服务
码上淘金6 小时前
【Python】Python常用控制结构详解:条件判断、遍历与循环控制
开发语言·python
Brilliant Nemo6 小时前
四、SpringMVC实战:构建高效表述层框架
开发语言·python
格林威8 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
小林学习编程8 小时前
SpringBoot校园失物招领信息平台
java·spring boot·后端