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 分钟前
Muduo:(3) 线程的封装,线程 ID 的获取、分支预测优化与信号量同步
c++·网络协议·架构·tcp
charliejohn20 分钟前
计算机考研 408 数据结构 中缀转后缀
数据结构·考研·算法
lifallen21 分钟前
后缀数组 (Suffix Array)
java·数据结构·算法
仰泳的熊猫27 分钟前
题目1523:蓝桥杯算法提高VIP-打水问题
数据结构·c++·算法·蓝桥杯
汉克老师44 分钟前
GESP2024年3月认证C++二级( 第三部分编程题(1) 乘法问题)
c++·算法·循环结构·gesp二级·gesp2级
白太岁1 小时前
Muduo:(0) 架构与接口总览
c++·架构·tcp
闻缺陷则喜何志丹1 小时前
微分中值定理与导数的应用
c++·高等数学·微分·中值定理·导数应用
tankeven1 小时前
HJ94 记票统计
c++·算法
雨翼轻尘1 小时前
2.1 链表1
数据结构·链表
mjhcsp2 小时前
C++ 树形 DP解析
开发语言·c++·动态规划·代理模式