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

相关推荐
@卞6 分钟前
高阶数据结构 --- 单调队列
数据结构·c++·算法
fpcc1 小时前
并行编程实战——CUDA编程的流的优先级
c++·cuda
勇闯逆流河3 小时前
【C++】C++11(下)
开发语言·c++
Live&&learn8 小时前
算法训练-数据结构
数据结构·算法·leetcode
胡萝卜3.09 小时前
掌握C++ map:高效键值对操作指南
开发语言·数据结构·c++·人工智能·map
电子_咸鱼9 小时前
【STL string 全解析:接口详解、测试实战与模拟实现】
开发语言·c++·vscode·python·算法·leetcode
风筝在晴天搁浅10 小时前
代码随想录 509.斐波那契数
数据结构·算法
落落落sss10 小时前
java实现排序
java·数据结构·算法
fei_sun11 小时前
【数据结构】2018年真题
数据结构
SundayBear11 小时前
C语言复杂类型声明完全解析:从右左原则到工程实践
c语言·开发语言·数据结构·嵌入式