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

相关推荐
蚊子码农1 小时前
算法题解记录--239滑动窗口最大值
数据结构·算法
额,不知道写啥。3 小时前
HAO的线段树(中(上))
数据结构·c++·算法
LYS_06183 小时前
C++学习(5)(函数 指针 引用)
java·c++·算法
ADDDDDD_Trouvaille4 小时前
2026.2.21——OJ95-97题
c++·算法
blackicexs4 小时前
第五周第七天
数据结构·算法
夏乌_Wx5 小时前
反转链表:三种实现思路与细节梳理
数据结构·链表
Once_day5 小时前
C++之《程序员自我修养》读书总结(4)
c语言·c++·编译和链接
紫陌涵光6 小时前
108.将有序数组转换为二叉搜索树
数据结构·算法·leetcode
tod1136 小时前
C++核心知识点全解析(二)
开发语言·c++·面试经验
载数而行5206 小时前
算法系列2之最短路径
c语言·数据结构·c++·算法·贪心算法