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

相关推荐
雾削木1 小时前
mAh 与 Wh:电量单位的深度解析
开发语言·c++·单片机·嵌入式硬件·算法·电脑
8RTHT3 小时前
数据结构(七)---链式栈
数据结构
Ethon_王3 小时前
走进Qt--工程文件解析与构建系统
c++·qt
Fency咖啡4 小时前
《代码整洁之道》第9章 单元测试 - 笔记
数据结构·b树
2501_906314324 小时前
优化无头浏览器流量:使用Puppeteer进行高效数据抓取的成本降低策略
开发语言·数据结构·数据仓库
C182981825754 小时前
项目中数据结构为什么用数组,不用List
数据结构
工藤新一¹4 小时前
C++/SDL进阶游戏开发 —— 双人塔防游戏(代号:村庄保卫战 13)
c++·游戏·游戏引擎·毕业设计·sdl·c++游戏开发·渲染库
让我们一起加油好吗4 小时前
【C++】类和对象(上)
开发语言·c++·visualstudio·面向对象
好想有猫猫5 小时前
【Redis】服务端高并发分布式结构演进之路
数据库·c++·redis·分布式·缓存
不是杠杠5 小时前
驼峰命名法(Camel Case)与匈牙利命名法(Hungarian Notation)详解
c++