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

相关推荐
2301_7779983416 分钟前
C/C++:预处理详解
c语言·c++
来一碗刘肉面18 分钟前
树的存储结构
数据结构
GrowthDiary0071 小时前
算法题:寻找二维数组top k问题
数据结构·python·算法
SHARK_pssm1 小时前
【数据结构——栈】
数据结构
阿米亚波2 小时前
【C++ STL】std::unordered_multiset
开发语言·数据结构·c++·笔记·stl
随意起个昵称2 小时前
贪心模型-Johnson法则
c++·算法
蜡笔小马2 小时前
【保姆级教程】Windows 下 CGAL 6.2 + VS2022 全流程实战:从源码编译到项目集成(附避坑指南)
c++·cgal
xiaoye-duck2 小时前
《Linux系统编程》Linux 系统多线程(八): C++ 高并发线程池全链路深度解析与从零手撕实现
linux·c++·线程池
胖大和尚3 小时前
在C++的类中,是否可以把函数声明成__device__
c++·cuda
-dzk-3 小时前
【链表】LC 160.相交链表
数据结构·链表