二叉树相关

二叉树相关

  • [力扣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);
    }
相关推荐
东离与糖宝12 分钟前
Java 26+Spring Boot 3.5,微服务启动从3秒压到0.8秒
java·人工智能
csdn_aspnet20 分钟前
C# 求n边凸多边形的对角线数量(Find number of diagonals in n sided convex polygon)
开发语言·算法·c#
禹中一只鱼34 分钟前
【力扣热题100学习笔记】 - 哈希
java·学习·leetcode·哈希算法
凌波粒35 分钟前
LeetCode--349.两个数组的交集(哈希表)
java·算法·leetcode·散列表
qq_2546744136 分钟前
Docker 中的 镜像(
开发语言
于先生吖43 分钟前
Java+SpringBoot 无人健身房物联网系统完整源码实现
java·spring boot·物联网
johnrui1 小时前
SpringBoot-JdbcTemplate
java·spring boot·后端
码云社区1 小时前
JAVA二手车交易二手车市场系统源码支持微信小程序+微信公众号+H5+APP
java·开发语言·微信小程序·二手交易·闲置回收
crescent_悦1 小时前
C++:The Largest Generation
java·开发语言·c++
indexsunny1 小时前
互联网大厂Java面试实战:从Spring Boot到微服务的技术问答解析
java·spring boot·redis·微服务·消息队列·电商