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

相关推荐
南风841112 小时前
C++ 四种强制类型转换详解
c++
微笑星河q13 小时前
C++STL【vector】常用接口函数使用和迭代器失效问题
开发语言·c++
HaiXCoder15 小时前
C++ 表达式怎么读:从一行代码到类型、值类别和生命周期
c++
老师我太想进步了202618 小时前
【数据结构-8】栈的实现
c语言·数据结构·算法
kyle~19 小时前
因子图优化(Factor Graph Optimization)
c++·算法·机器人·传感器
geats人山人海19 小时前
现代c++4.1
java·开发语言·c++
lunar_coder21 小时前
【算法】前缀和:把 O(n) 的重复劳动变成 O(1) 的一次查询
c++·算法
青少儿编程课堂21 小时前
第十八届蓝桥杯青少组图形化 + 全国青少年信息素养大赛 + 电子学会图形化考级:校园三消乐大挑战解析
c++·python·算法·bfs·信息学竞赛
青少儿编程课堂1 天前
蓝桥杯·全国青少年信息素养大赛·电子学会Python/C++考级:网格连通块与岛屿数量(DFS+BFS双解)
c++·python·算法·bfs·信息学竞赛
码客-ygluu1 天前
X3 LUA 异步分布式微服务集群框架
c++·架构·lua