104. Maximum Depth of Binary Tree

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:

Input: root = [1,null,2]
Output: 2

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • -100 <= Node.val <= 100
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==nullptr)return 0;
        return max(maxDepth(root->left),maxDepth(root->right))+1;
    }
};

相关推荐

  1. 面试经典150题(101-104)

    2024-06-06 18:42:06       44 阅读
  2. LeetCode 1045, 14, 25

    2024-06-06 18:42:06       34 阅读
  3. 力扣爆刷第104天之CodeTop100五连刷6-10

    2024-06-06 18:42:06       43 阅读
  4. DAY 10 | 1047, (20,150)

    2024-06-06 18:42:06       58 阅读
  5. KY104 Pre-Post

    2024-06-06 18:42:06       64 阅读
  6. PYTHON 120道题目详解(100-102

    2024-06-06 18:42:06       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 18:42:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 18:42:06       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 18:42:06       82 阅读
  4. Python语言-面向对象

    2024-06-06 18:42:06       91 阅读

热门阅读

  1. Nginx 实战-01-nginx ubuntu(windows WSL2) 安装笔记

    2024-06-06 18:42:06       31 阅读
  2. 很多Oracle中的SQL语句在EF中写不出来

    2024-06-06 18:42:06       27 阅读
  3. L1-003 个位数统计

    2024-06-06 18:42:06       29 阅读
  4. t1t1t1t1

    2024-06-06 18:42:06       30 阅读
  5. MongoDB管理内存使用

    2024-06-06 18:42:06       35 阅读
  6. 好的一些网安资源

    2024-06-06 18:42:06       26 阅读
  7. PostgreSql创建数据库,用户以及权限分配

    2024-06-06 18:42:06       31 阅读
  8. Flutter 中的 RawImage 小部件:全面指南

    2024-06-06 18:42:06       30 阅读
  9. FFS in Unix

    2024-06-06 18:42:06       30 阅读
  10. ElementUi Table复选框回显

    2024-06-06 18:42:06       38 阅读
  11. 设计模式深度解析:分布式与中心化

    2024-06-06 18:42:06       23 阅读
  12. GPT-4o详解

    2024-06-06 18:42:06       26 阅读
  13. Redis常见异常及优化方案

    2024-06-06 18:42:06       27 阅读
  14. 求解数组中N数之和最接近目标值的算法详解

    2024-06-06 18:42:06       27 阅读