二叉树相关

二叉树相关

  • [力扣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);
    }
相关推荐
.YM.Z1 小时前
优选算法——【双指针算法2】C++实现
开发语言·c++·算法
逆向编程1 小时前
MATLAB高级数据结构:cell、struct与table深度应用
开发语言·数据结构·matlab
Wang's Blog1 小时前
Java 项目实战:交友项目 - 即时通信总结:全链路复盘、可复用清单与自检
java·python·交友
文丑颜不良啊1 小时前
LeetCode1501~1520题目解析
java·leetcode
努力努力再努力wz2 小时前
【Lua入门系列】从 Table 到元表与元方法:一文理解 Lua 的行为扩展机制
开发语言·junit·lua
qq_406981032 小时前
什么是顶层 const什么是底层const
开发语言·c++
Eider19982 小时前
python基础语法之并发基础练习
开发语言·python
长不大的杨瑞杰2 小时前
OpenCode v2 LSP 实战:内置语言服务器、自定义配置与诊断闭环
java·人工智能·typescript·lsp·opencode
#麻辣小龙虾#2 小时前
网页布局css中flex+margin的组合布局技巧
开发语言·前端·javascript
云和数据.ChenGuang2 小时前
python和java的try
java·开发语言·python