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

相关推荐
苏宸啊26 分钟前
rbtree封装map和set
c++
汉克老师1 小时前
GESP2025年6月认证C++三级( 第一部分选择题(1-8))
c++·二进制·原码·补码·gesp三级·gesp3级·八进制、
不想写代码的星星1 小时前
C++ 折叠表达式:“我写递归你写折叠,咱俩代码差十年”
c++
Titan20242 小时前
map和set的封装学习笔记
数据结构·c++
懒惰的bit2 小时前
MFC常见消息映射(简洁版)
c++·mfc
Yupureki2 小时前
《算法竞赛从入门到国奖》算法基础:动态规划-路径dp
数据结构·c++·算法·动态规划
321.。3 小时前
Linux 进程控制深度解析:从创建到替换的完整指南
linux·开发语言·c++·学习
小Tomkk3 小时前
怎么配置 Visual Studio Code 配置 C/C++
c语言·c++·vscode
CheerWWW3 小时前
C++学习笔记——枚举、继承、虚函数、可见性
c++·笔记·学习
比昨天多敲两行3 小时前
C++ AVL树
开发语言·c++