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

相关推荐
liulilittle14 分钟前
C++ 并发双阶段队列设计原理与实现
linux·开发语言·c++·windows·算法·线程·并发
森G24 分钟前
五、Linux字符设备驱动
linux·arm开发·c++·ubuntu
繁星蓝雨36 分钟前
我与C++的故事(杂谈)
开发语言·c++
Jasmine_llq38 分钟前
《P3811 【模板】模意义下的乘法逆元》
数据结构·算法·线性求逆元算法·递推求模逆元
虹科网络安全40 分钟前
艾体宝干货 | Redis Java 开发系列#2 数据结构
java·数据结构·redis
sin_hielo1 小时前
leetcode 2211
数据结构·算法·leetcode
Queenie_Charlie1 小时前
和为k的连续区间
数据结构·c++·map
爱学java的ptt2 小时前
206反转链表
数据结构·链表
java修仙传2 小时前
力扣hot100:最大子数组和
数据结构·算法·leetcode
hweiyu002 小时前
数据结构:二叉树
数据结构