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

相关推荐
一个不知名程序员www5 小时前
算法学习入门 --- 哈希表和unordered_map、unordered_set(C++)
c++·算法
C++ 老炮儿的技术栈5 小时前
在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
c语言·c++·windows·git·vscode·visual studio
%xiao Q6 小时前
GESP C++五级-202406
android·开发语言·c++
Sarvartha6 小时前
C++ STL 栈的便捷使用
c++·算法
Aevget7 小时前
MFC扩展库BCGControlBar Pro v37.2 - 全新的VS 2026可视化管理器
c++·mfc·bcg·界面控件·ui开发
bubiyoushang8887 小时前
基于CLEAN算法的杂波抑制Matlab仿真实现
数据结构·算法·matlab
C+-C资深大佬7 小时前
C++类型判断
开发语言·c++
曾经的三心草7 小时前
redis-2-数据结构内部编码-单线程-String命令
数据结构·数据库·redis
Yu_Lijing7 小时前
基于C++的《Head First设计模式》笔记——模式合作
c++·笔记·设计模式
zmzb01037 小时前
C++课后习题训练记录Day74
开发语言·c++