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

相关推荐
OPEN-F38 分钟前
C++进阶教程:继承与多态
开发语言·c++
luj_17682 小时前
大律师考核应重能力与科技素养
c语言·开发语言·c++·经验分享·算法
刃神太酷啦3 小时前
Linux 系统 MySQL 完整安装配置教程:从卸载 MariaDB 到优化 my.cnf----《Hello MySQL!》(1)
android·linux·c语言·c++·mysql·leetcode·mariadb
带多刺的玫瑰3 小时前
Leecode#9刷题之回文数
数据结构·算法
萌动的小火苗4 小时前
数据结构面试题【无答案】
c语言·开发语言·数据结构·链表
汉克老师6 小时前
CSP-J 初赛(以满分为目标):第十五课《结构体、排序与自定义比较——从“给数字排队”到“给一群人排队”》
c++·csp-j·小学生·学c++编程
tianyu2347 小时前
HashMap 灵魂 30 问:从数据结构到红黑树,从扩容到 ConcurrentHashMap,一篇彻底通关
数据结构·hashmap·hashset·linkedhashmap‌
HugoStudio_SWAN7 小时前
C++ CMD 互动动画:按键触发爆炸效果
开发语言·c++·学习·程序人生
水饺编程7 小时前
第5章,[Win32 章节] :Polygon 函数和多边形填充模式
c语言·c++·windows·visual studio
wuminyu7 小时前
Java FFM处理网络读写事件源码剖析
java·linux·c语言·jvm·c++