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

相关推荐
勇闯逆流河4 小时前
【数据结构】堆
c语言·数据结构·算法
jjkkzzzz4 小时前
Linux下的c/c++开发之操作Redis数据库
数据库·c++·redis
pystraf4 小时前
LG P9844 [ICPC 2021 Nanjing R] Paimon Segment Tree Solution
数据结构·c++·算法·线段树·洛谷
Funny-Boy5 小时前
菱形继承原理
c++
Nobkins6 小时前
2021ICPC四川省赛个人补题ABDHKLM
开发语言·数据结构·c++·算法·图论
海棠蚀omo6 小时前
C++笔记-红黑树
开发语言·c++·笔记
一个Potato7 小时前
C++笔试题(金山科技新未来训练营):
c++·科技
休息一下接着来7 小时前
C++ I/O多路复用
linux·开发语言·c++
龙湾开发7 小时前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 12.曲面细分
c++·笔记·学习·3d·图形渲染
darkchink8 小时前
[LevelDB]LevelDB版本管理的黑魔法-为什么能在不锁表的情况下管理数据?
c语言·数据库·c++·oracle·数据库开发·dba·db