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

相关推荐
自由生长20241 天前
为什么C++项目偏爱.cxx扩展名:从MongoDB驱动说起
c++
CSDN_RTKLIB1 天前
【GNU、GCC、g++、MinGW、MSVC】上
c++·gnu
b***74881 天前
C++在系统中的内存对齐
开发语言·c++
散峰而望1 天前
C++数组(三)(算法竞赛)
开发语言·c++·算法·github
4***14901 天前
C++在系统中的编译优化
开发语言·c++
mit6.8241 天前
[HomeKey] 握手协议 | NFC协议处理器
c++
oioihoii1 天前
C++程序执行起点不是main:颠覆你认知的真相
开发语言·c++
hetao17338371 天前
2025-11-25~26 hetao1733837的刷题记录
c++·算法
历程里程碑1 天前
各种排序法大全
c语言·数据结构·笔记·算法·排序算法
u***u6851 天前
C++在系统中的异常处理
java·开发语言·c++