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

相关推荐
AI人工智能+电脑小能手4 小时前
【大白话说Java面试题】【Java基础篇】第15题:JDK1.7中HashMap扩容为什么会发生死循环?如何解决
java·开发语言·数据结构·后端·面试·哈希算法
张健11564096486 小时前
使用信号量限制并发数量
开发语言·c++
jc06206 小时前
6.1云原生之Docker
c++·docker·云原生
Mrlxl.cn7 小时前
计算机网络——网络层
c语言·数据结构·计算机网络·考研
寒秋花开曾相惜8 小时前
(学习笔记)4.2 逻辑设计和硬件控制语言HCL(4.2.1 逻辑门&4.2.2 组合电路和HCL布尔表达式)
linux·网络·数据结构·笔记·学习·fpga开发
叶子野格8 小时前
《C语言学习:指针》12
c语言·开发语言·c++·学习·visual studio
Fuyo_11199 小时前
C++ 内存管理
c++·笔记
澈20710 小时前
C++面向对象:类与对象核心解析
c++·算法
6Hzlia10 小时前
【Hot 100 刷题计划】 LeetCode 141. 环形链表 | C++ 哈希表直觉解法
c++·leetcode·链表
码完就睡10 小时前
数据结构——哈希表原理与C语言实现总结
数据结构·散列表