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

相关推荐
choumin35 分钟前
创建型模式——原型模式
c++·设计模式·原型模式·创建型模式
王维同学2 小时前
Credential Provider、Filter 与 PLAP 的 CLSID 枚举
c++·windows·安全·注册表
Carlgood-Minecraft3 小时前
随机分位系统破解版 (BPS)Version-B4.2
c++
choumin3 小时前
结构型模式——装饰模式
c++·设计模式·装饰模式·结构型模式
冻柠檬飞冰走茶4 小时前
PTA基础编程题目集 7-7 12-24小时制(C语言实现)
c语言·开发语言·数据结构·算法
雨落在了我的手上4 小时前
Java数据结构(八):双链表的实现
数据结构
paeamecium4 小时前
【PAT甲级真题】- Kuchiguse (20)
数据结构·c++·python·算法·pat考试·pat
青山木4 小时前
Hot 100 --- 全排列
java·数据结构·算法·leetcode·深度优先
txzrxz5 小时前
拓补排序讲解
c++·算法·图论·排序
小保CPP6 小时前
OpenCV C++基于模糊数学的图像滤波
c++·人工智能·opencv·计算机视觉