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

相关推荐
小跌—2 小时前
Redis数据结构和单线程
数据结构·数据库·redis
草莓熊Lotso3 小时前
Linux 磁盘基础:从物理结构到 CHS/LBA 寻址,吃透数据存储底层逻辑
linux·运维·服务器·c++·人工智能
燃于AC之乐3 小时前
深入解剖STL map/multimap:接口使用与核心特性详解
开发语言·c++·stl·面试题·map·multimap
草莓熊Lotso3 小时前
Qt 核心事件系统全攻略:鼠标 / 键盘 / 定时器 / 窗口 + 事件分发与过滤
运维·开发语言·c++·人工智能·qt·ui·计算机外设
阿kun要赚马内5 小时前
C++中的Windows API双缓冲技术
c++
WBluuue11 小时前
Codeforces 1078 Div2(ABCDEF1)
c++·算法
学无止境_永不停歇11 小时前
十、C++多态
开发语言·c++
老歌老听老掉牙12 小时前
QT开发踩坑记:按钮点击一次却触发两次?深入解析信号槽自动连接机制
c++·qt
橘色的喵12 小时前
现代 C++17 相比 C 的不可替代优势
c语言·c++·现代c++·c++17
浅念-12 小时前
C/C++内存管理
c语言·开发语言·c++·经验分享·笔记·学习