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

相关推荐
小飞学编程...1 天前
【哈希表】
数据结构·哈希算法·散列表
码匠许师傅1 天前
【C++ 面试真题】26. 聊聊 C++ 的智能指针
java·c++·面试
重生之后端学习1 天前
283. 移动零[简单]✅
开发语言·数据结构·算法·leetcode·职场和发展
动词ing1 天前
【C语言】结构体+文件基础
c语言·开发语言·数据结构
夜不会漫长1 天前
C++入门(1)
开发语言·c++·算法
晚风醉蝶1 天前
1-11-奇偶排序-OddEvenSort
java·数据结构·算法
旖旎夜光1 天前
LeetCode 852:山脉数组的峰顶索引(二分查找) —— 题解
数据结构·c++·算法·leetcode·二分查找
..Dauntless..1 天前
【C++】继承——基类和派生类的配合
开发语言·c++·算法
程与留1 天前
05_Qt 核心模块概览——Qt Core、Gui、Widgets、Quick 的职责划分
c++·qt
buhuizhiyuci1 天前
【QT-百日筑基篇】修真世界摸爬滚打多年,终于黄天不负有心人,突破炼器中期——常用控件QWight的属性
开发语言·c++·qt·gui·图形化