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

相关推荐
坚持就完事了6 分钟前
Java实现数据结构中的链表
java·数据结构·链表
写代码的小球8 分钟前
C++ 标准库 <numbers>
开发语言·c++·算法
拳里剑气12 分钟前
C++:哈希
开发语言·数据结构·c++·算法·哈希算法·学习方法
闻缺陷则喜何志丹14 分钟前
【高等数学】导数与微分
c++·线性代数·算法·矩阵·概率论
智者知已应修善业14 分钟前
【项目配置时间选择自己还是团体】2025-3-31
c语言·c++·经验分享·笔记·算法
闻缺陷则喜何志丹14 分钟前
【分组背包】P12316 [蓝桥杯 2024 国 C] 循环位运算|普及+
c++·算法·蓝桥杯·洛谷·分组背包
24白菜头21 分钟前
2026-2-9:LeetCode每日一题(动态规划专项)
数据结构·笔记·学习·算法·leetcode
BOTTLE_平27 分钟前
C++图论全面解析:从基础概念到算法实践
c++·算法·图论
Lenyiin27 分钟前
《 C++ 修炼全景指南:二十四 》彻底攻克图论!轻松解锁最短路径、生成树与高效图算法
c++·算法·图论·邻接表·邻接矩阵·最小生成树·最短路径
瓦特what?29 分钟前
冒 泡 排 序
开发语言·数据结构·c++