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

相关推荐
dialect_3 小时前
KMP算法
开发语言·c++·算法
漂流瓶jz4 小时前
UVA-1612 猜名次 题解答案代码 算法竞赛入门经典第二版
数据结构·算法·题解·排序·aoapc·算法竞赛入门经典
ShineWinsu5 小时前
对于Qt:QRadioButton常见属性以及QButtonGroup的解析
数据库·c++·qt·microsoft·gui·qradiobutton·qbuttongroup
傲世仙尊6 小时前
多态-虚表才活到运行期符号表早已消失
c++
chenbingjie_c6 小时前
C++ 多态完全指南:从虚函数重写到虚函数表原理
开发语言·c++
是main不是漫6 小时前
【双指针——移动零】数组划分类题目的入门基石
c++·算法
·present·6 小时前
CSAPP(Bomb lab)
c语言·c++
王老师青少年编程6 小时前
2026年信奥赛c++普及组csp-j第二轮复赛真题
c++·真题·csp·信奥赛
时间的拾荒人7 小时前
CMake 常用命令详解:从版本声明到安装部署
c++·面试
D e re k ZH10 小时前
P5025 [SNOI2017] 炸弹
c++·算法