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

相关推荐
nianniannnn33 分钟前
Eigen 矩阵操作笔记
c++·笔记·线性代数·矩阵
adfass33 分钟前
桌面挂件时钟/多功能时钟C++
开发语言·c++·算法
全栈视界师41 分钟前
《机器人实践开发②:Foxglove 嵌入式移植 + CMake 集成》
c++·机器人·数据可视化
小熳芋1 小时前
排序链表- python-非进阶做法
数据结构·算法·链表
繁华似锦respect1 小时前
Linux-内核核心组成部分
linux·c++
zore_c1 小时前
【C语言】数据在内存中的存储(超详解)
c语言·开发语言·数据结构·经验分享·笔记
程序员-周李斌1 小时前
ArrayList 源码深度分析(基于 JDK 8)
java·开发语言·数据结构·算法·list
不知所云,2 小时前
2.windows c/c++ 编译器安装, mingw和clang
c语言·c++·windows·mingw·clang·c编译器
爪哇部落算法小助手2 小时前
爪哇周赛 Round 3
数据结构·c++·算法
十五年专注C++开发2 小时前
Mimalloc:一款高性能、低开销和线程安全的C++内存分配器
c++·内存分配·mimalloc