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

相关推荐
byte轻骑兵8 小时前
【C++特殊工具与技术】优化内存分配(四):定位new表达式、类特定的new、delete表达式
开发语言·c++
广州正荣9 小时前
成绩管理革新者:C++驱动的智能数据处理平台
c++·人工智能·科技
90wunch9 小时前
对象回调初步研究
c++·windows·安全
Se_ren_di_pity9 小时前
C++ STL容器汇总
开发语言·c++
Wendy_robot9 小时前
【零基础勇闯嵌入式岗】从单片机低功耗中获得的启发
c++·单片机·嵌入式硬件
零叹10 小时前
篇章十 数据结构——排序
java·数据结构·算法·排序算法
朝朝又沐沐11 小时前
算法竞赛阶段二-数据结构(32)数据结构简单介绍
数据结构·算法
梦境虽美,却不长11 小时前
数据结构 (树) 学习 2025年6月12日12:59:39
数据结构·学习·二叉树·霍夫曼树·非二叉树
共享家952711 小时前
c语言(重点)
c语言·数据结构·算法
玉米的玉*」*11 小时前
【每日likou】704. 二分查找 27. 移除元素 977.有序数组的平方
数据结构·算法·leetcode