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

相关推荐
AA陈超7 小时前
ASC学习笔记0020:用于定义角色或Actor的默认属性值
c++·笔记·学习·ue5·虚幻引擎
coderxiaohan8 小时前
【C++】仿函数 + 模板进阶
开发语言·c++
Kuo-Teng8 小时前
LeetCode 279: Perfect Squares
java·数据结构·算法·leetcode·职场和发展
CoderYanger9 小时前
B.双指针——3194. 最小元素和最大元素的最小平均值
java·开发语言·数据结构·算法·leetcode·职场和发展·1024程序员节
SalvoGao9 小时前
Python学习 | 怎么理解epoch?
数据结构·人工智能·python·深度学习·学习
思成不止于此9 小时前
深入理解 C++ 多态:从概念到实现的完整解析
开发语言·c++·笔记·学习·多态·c++40周年
布丁写代码11 小时前
GESP C++ 一级 2025年09月真题解析
开发语言·c++·程序人生·学习方法
喵个咪12 小时前
Qt 优雅实现线程安全单例模式(模板化 + 自动清理)
c++·后端·qt
兩尛13 小时前
215. 数组中的第K个最大元素
数据结构·算法·排序算法
9523613 小时前
数据结构-堆
java·数据结构·学习·算法