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

相关推荐
福楠12 小时前
C++ STL | list
c语言·开发语言·数据结构·c++·算法·list
myloveasuka12 小时前
int类型的取值范围(为什么负数比正数表示的范围多一位)
c语言·c++
玉树临风ives12 小时前
atcoder ABC439 题解
c++·算法
程序员zgh12 小时前
类AI技巧 —— 文字描述+draw.io 自动生成图表
c语言·c++·ai作画·流程图·ai编程·甘特图·draw.io
红豆诗人12 小时前
算法和数据结构--时间复杂度和空间复杂度
数据结构·算法
阿豪只会阿巴12 小时前
【多喝热水系列】从零开始的ROS2之旅——Day5
c++·笔记·python·ubuntu·ros2
郑泰科技12 小时前
fmm(快速地图匹配)实践:Boost header not found解决方案
c++·windows·交通物流
维C泡泡12 小时前
STL(初识string)
开发语言·c++
黎雁·泠崖12 小时前
栈与队列之栈入门攻略:从核心概念到数组实现
c语言·数据结构
郝学胜-神的一滴12 小时前
Linux线程使用注意事项:骈文技术指南
linux·服务器·开发语言·数据结构·c++·程序人生