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

相关推荐
David猪大卫6 小时前
【C++修炼】智能指针使用及原理
开发语言·c++·经验分享·笔记·学习·考研·面试
零衣贰7 小时前
The 2026 ICPC Asia East Continent Online Contest (I) 题解
c++·icpc
不会就选b7 小时前
算法日常・每日刷题--<贪心>7
数据结构·算法·leetcode
fpcc8 小时前
c++编程实践—统一初始化
服务器·c++
Zenova EdgeOS9 小时前
C++ 工业边缘 libcurl HTTP 客户端实战
开发语言·c++·网络协议·边缘计算
爱吃苹果的日记本9 小时前
数据结构第一课
c语言·数据结构·数据库·学习·c#
Jackson_GJH9 小时前
Qt 右键自定义菜单的实现
开发语言·c++·qt
张小姐的猫10 小时前
【AI大模型接入SDK】 —— Gemini接入封装
android·数据结构·数据库·c++·人工智能·python
一木 之林10 小时前
七、一-AI 工程实践、插件化调试与软件交付
java·linux·c++
闲坐含香咀翠11 小时前
从 132MB 到 25MB:列式存储怎么把 CPU 缓存命中率提上去的
前端·数据结构·性能优化