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

相关推荐
无忧.芙桃10 分钟前
C++语言原理与实践(十二):list类的底层实现
c++·windows·list
沫璃染墨12 分钟前
《Linux工程实践篇(二):线程池实战——从线程安全单例到完整源码实现》
linux·开发语言·c++·单例模式·设计模式·系统架构
有只小白叫岳飒1 小时前
c++智能指针 unique_ptr与函数调用
c++
Leo.yuan1 小时前
Kafka+Flink拼凑,还是FineDataLink 5.0一体化?实时数据处理的两种思路
c++·mfc
flinn_small1 小时前
三分算法:原理、实现与实战应用
数据结构·算法·三分
INGNIGHT2 小时前
944 · 最大子矩阵(前缀和)
数据结构·c++·算法
All for pursuit.3 小时前
【动态规划-6】96.不同的二叉搜索树
数据结构·c++·算法·leetcode·动态规划
信奥卷王3 小时前
[GESP202609 六级] 数组划分
数据结构·算法
大鹏的NLP博客4 小时前
C++ 内存安全的未来:从 Safety Profiles 到 Safe C++
c++·内存安全
程序员zgh4 小时前
C++ erase() 函数用法
c语言·开发语言·c++·学习