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

相关推荐
脱胎换骨-军哥22 分钟前
C++/Rust无缝互操作:混合系统新常态
开发语言·c++·rust
ChaoZiLL1 小时前
我的数据结构4-栈和队列
数据结构
miller-tsunami1 小时前
顺序表相关知识点
数据结构·顺序表
胖大和尚3 小时前
C++ 多线程编程的实现方式
c++·thread
在水一缸4 小时前
深入浅出 Catch2:现代 C++ 测试框架的优雅实践
开发语言·c++·单元测试·log4j·测试框架·catch2
华玥作者4 小时前
uniapp 万条数据不卡顿:我写了个虚拟列表组件 hy-list,原生支持瀑布流
数据结构·uni-app·list·vue3
2401_841495644 小时前
【数据结构】B*树
数据结构·c++·b树·算法·删除·插入·三分分裂
不如语冰5 小时前
AI大模型入门-模块导入import
数据结构·人工智能·pytorch·python
斐夷所非5 小时前
在纷繁竞逐中稳步前行:C++ 2006–2020
c++
岑梓铭5 小时前
《考研408数据结构》第七章(7.1 查找:顺序查找、折半查找、分块查找)复习笔记
数据结构·笔记·考研·408·ds·查找