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

相关推荐
一直在努力的小宁27 分钟前
《代码随想录-精华内容提取》07 二叉树
数据结构·算法·链表·面试
江塘29 分钟前
机器学习-决策树多种生成方法讲解及实战代码讲解(C++/Python实现)
c++·python·决策树·机器学习
多彩电脑34 分钟前
死循环逻辑检测
数据结构·python·算法·动态规划
初见无风40 分钟前
4.4 Boost库工具类assign 的使用
开发语言·c++·boost
月夜的风吹雨1 小时前
【C++ STL容器适配器】:解密Stack、Queue与Priority Queue的设计智慧
开发语言·c++·stl·优先级队列··队列·适配器
二川bro1 小时前
第48节:WebAssembly加速与C++物理引擎编译
java·c++·wasm
2501_941111931 小时前
基于C++的区块链实现
开发语言·c++·算法
hetao17338371 小时前
2025-11-16~17 hetao1733837的刷题记录
c++·算法
_OP_CHEN1 小时前
算法基础篇:(九)贪心算法拓展之推公式:从排序规则到最优解的推导艺术
c++·算法·贪心算法·推公式·算法竞赛·acm/icpc
czxyvX1 小时前
010-C++之List
开发语言·c++·list