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;
    }
    };

相关推荐
t***54431 分钟前
Clang 编译器在 Orwell Dev-C++ 中的局限性
开发语言·c++
Sam_Deep_Thinking1 小时前
学数据结构到底有什么用
数据结构
yolo_guo2 小时前
redis++使用: hmset 与 hmget
c++·redis
handler013 小时前
拒绝权限报错!三分钟掌握 Linux 权限管理
linux·c语言·c++·笔记·学习
t***5444 小时前
如何在Dev-C++中选择Clang编译器
开发语言·c++
汉克老师4 小时前
GESP2023年9月认证C++三级( 第一部分选择题(9-15))
c++·gesp三级·gesp3级
py有趣5 小时前
力扣热门100题之和为K的子数组
数据结构·算法·leetcode
hipolymers5 小时前
C语言怎么样?难学吗?
c语言·数据结构·学习·算法·编程
CS创新实验室5 小时前
从“跑得动”到“跑得稳”:深度剖析数据结构究竟是理论点缀还是核心战力?
数据结构
jllllyuz6 小时前
MATLAB 蒙特卡洛排队等待模拟程序
数据结构·matlab