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

相关推荐
2601_956121972 小时前
背包基础篇(01、完全、分组、多重、混合)
c++·算法·动态规划
fpcc2 小时前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
ShineWinsu6 小时前
对于C++:C++20中线程、初始化、Lambda与内存视图等特性的解析
c++·c++20
张张的快乐时光呀!7 小时前
记录使用 MFC 打开文件对话框读取一张图片并做简单的灰度转换算法简单学习过程
c++·mfc
东华万里9 小时前
第41篇 C++类与对象核心知识梳理:从底层原理到面试实战
开发语言·c++·大学生专区
qq_5896660510 小时前
C语言、C++与C#的区别详解
c语言·c++·c#
万法若空10 小时前
排列组合恒等式
c++·算法
一直C10 小时前
【数据结构】哈希表+算法复杂度与经典排序查找(C语言)
java·linux·开发语言·数据结构·算法·ubuntu·散列表
淡海水10 小时前
04-02-哈希-Dictionary-TKey-TValue-上-核心数据结构
数据结构·算法·c#·哈希算法·编译·字典·dictionary
老洋葱Mr_Onion11 小时前
【C++】CSP-J初赛模拟卷七错题整理(作者自用)
c++·算法·深度优先