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

相关推荐
水云桐程序员3 小时前
C++可以写手机应用吗
开发语言·c++·智能手机
炸膛坦客7 小时前
嵌入式 - 数据结构与算法:(1-7)数据结构 - 顺序表和链表的对比
数据结构·链表
小黄人软件7 小时前
C++读写编辑CSV文件示例源码 用于数据导入导出,比Excel好使
开发语言·c++·excel
郭涤生8 小时前
C++各个版本的性能和安全性总结
开发语言·c++
hoiii1878 小时前
基于栅格法的机器人工作空间划分系统
数据结构·机器人
wljy19 小时前
二、静态库的制作和使用
linux·c语言·开发语言·c++
道剑剑非道9 小时前
FFmpeg 6.0 实战:用 C++ 封装摄像头采集与 RTSP 推流
开发语言·c++·ffmpeg
光电笑映9 小时前
从环境变量到进程虚拟地址空间——Linux 内存管理的底层脉络
linux·服务器·c++·c
sparEE10 小时前
c++字符串和自定义字面量
开发语言·c++
蜡笔小马11 小时前
03.C++设计模式-原型模式
c++·设计模式·原型模式