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

相关推荐
kiracrimson9 小时前
从缓存的角度看链表与线性表的差异
数据结构·链表·缓存
心抵鹊10 小时前
归并排序之翻转对(hard)
数据结构·算法
_Twink1e11 小时前
openJiuwen 实训营 Day 1 · WorkSwarm 入门教程
开发语言·c++·openjiuwen
dong_junshuai11 小时前
每天一个开源项目#87 fmt:24.5K Stars 的 C++ 格式化核心
c++·开源·github
雪的季节12 小时前
Multisim14.0的详细中文安装步骤【附安装包】
c++
Persistent的粽子!13 小时前
C++:类与对象(二)
开发语言·c++
白狐_79813 小时前
408 数据结构|红黑树插入:只记两大类
数据结构
蒸蒸yyyyzwd14 小时前
cpp 选手备战秋招学习笔记 day23
c++·求职招聘
.YM.Z14 小时前
C++ STL 容器深度剖析:vector 与 list 的模拟实现及对比
开发语言·c++·vector·list
mengzhi啊15 小时前
QEventLoop loop配合loop.exec();替代while(1)。电脑cpu占用为0,使用while(1)cpu占用率100%
c++·qt