【算法题】104. 二叉树的最大深度

题目

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

示例 1:

输入:root = [3,9,20,null,null,15,7]
输出:3
示例 2:

输入:root = [1,null,2]
输出:2

提示:

树中节点的数量在 [0, 104] 区间内。
-100 <= Node.val <= 100

题解

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

来自力扣官方题解

最近更新

  1. TCP协议是安全的吗?

    2024-02-16 11:34:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-16 11:34:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-16 11:34:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-16 11:34:01       18 阅读

热门阅读

  1. Flutter run 一直 Running Gradle task ‘assembleDebug’…

    2024-02-16 11:34:01       31 阅读
  2. RedisTemplate重写的一些模板

    2024-02-16 11:34:01       34 阅读
  3. LeetCode 399:除法求值(图的bfs遍历)

    2024-02-16 11:34:01       32 阅读
  4. 力扣102-二叉树的层序遍历

    2024-02-16 11:34:01       31 阅读
  5. 蓝桥杯(Web大学组)2022省赛真题:冬奥大抽奖

    2024-02-16 11:34:01       31 阅读
  6. 代码随想录算法训练营29期Day51|LeetCode 139

    2024-02-16 11:34:01       37 阅读
  7. vue3跨组件(多组件)通信:事件总线【Event Bus】

    2024-02-16 11:34:01       34 阅读
  8. GEE:关于在GEE平台上进行回归计算的若干问题

    2024-02-16 11:34:01       36 阅读
  9. Ubuntu+Anaconda 常用指令记录

    2024-02-16 11:34:01       30 阅读
  10. Ajax,

    2024-02-16 11:34:01       30 阅读