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 分钟前
单调栈、单调队列(模板)、子矩阵(模板)
数据结构·算法·蓝桥杯
不会就选b12 分钟前
算法日常・每日刷题--<快速排序>2
数据结构·算法
凯瑟琳.奥古斯特32 分钟前
力扣1012数位DP解法详解
开发语言·c++·算法·leetcode·职场和发展
chh5631 小时前
C++--string
java·开发语言·网络·c++·学习
kyle~1 小时前
Schema---数据结构的形式化规则描述
数据结构·windows·microsoft
J_yyy1 小时前
基于Reactor的文件管理服务开发笔记
c++·笔记·reactor·多线程编程·muduo
李小小钦2 小时前
D. Storming Arasaka(Codeforces 2238)
c语言·开发语言·数据结构·c++·算法
卷无止境2 小时前
SFML 深度解读:一个教科书级 C++ 多媒体库的内功心法
c++·后端
先吃饱再说3 小时前
一篇吃透树的遍历:递归与迭代的完整拆解
数据结构·算法
jinyishu_3 小时前
C++ 类和对象
开发语言·c++