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

相关推荐
L_09078 小时前
【C++】异常
开发语言·c++
liulilittle8 小时前
关于拥塞控制的几点思考
网络·c++·tcp/ip·计算机网络·信息与通信·tcp·通信
QT-Neal10 小时前
C++ 编码规范
c++
啦啦啦啦啦zzzz10 小时前
数据结构:红黑树理论
数据结构·c++·红黑树
Yolo_TvT11 小时前
C++:默认构造函数
c++
San813_LDD11 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
小欣加油12 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
.千余13 小时前
【C++】手写双向链表:list容器模拟实现
开发语言·c++·笔记·学习·其他
liulilittle13 小时前
过冲:拥塞控制的呼吸与盲行
linux·网络·c++·tcp/ip·计算机网络·tcp·通信
Felven13 小时前
B. Fair Numbers
数据结构·算法