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

相关推荐
yzx99101326 分钟前
Python数据结构入门指南:从基础到实践
开发语言·数据结构·python
消失的旧时光-19431 小时前
智能指针(二):机制篇 —— 移动语义与所有权转移
c++·智能指针
ValhallaCoder1 小时前
hot100-堆
数据结构·python·算法·
风吹乱了我的头发~2 小时前
Day31:2026年2月21日打卡
开发语言·c++·算法
mjhcsp2 小时前
C++ 后缀平衡树解析
android·java·c++
D_evil__2 小时前
【Effective Modern C++】第六章 lambda表达式:33. 对于auto&&形参使用decltype以及forward它们
c++
-Rane4 小时前
【C++】vector
开发语言·c++·算法
希望之晨4 小时前
c++ 11 学习 override
开发语言·c++·学习
消失的旧时光-19434 小时前
智能指针(四):体系篇 —— 现代 C++ 内存管理全景图
开发语言·c++
仰泳的熊猫4 小时前
题目1531:蓝桥杯算法提高VIP-数的划分
数据结构·c++·算法·蓝桥杯