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

相关推荐
BirdenT1 小时前
20260519紫题训练
c++·算法
谙弆悕博士6 小时前
【附C源码】从零实现C语言堆数据结构:原理、实现与应用
c语言·数据结构·算法··数据结构与算法
C+++Python8 小时前
C++ 进阶学习完整指南
java·c++·学习
sparEE8 小时前
c++值类别、右值引用和移动语义
开发语言·c++
jrrz08289 小时前
Apollo MPC Controller
c++·自动驾驶·apollo·mpc·横向控制·lateral control
小王C语言11 小时前
【线程概念与控制】:线程封装
jvm·c++·算法
学习,学习,在学习11 小时前
Qt工控仪器程序框架设计详解(工控多仪器控制版本)
开发语言·c++·qt
信竞星球_少儿编程题库12 小时前
2026年全国信息素养大赛算法应用主题赛 丝路新城 C++ 模拟卷(三)
开发语言·c++
Zhang~Ling12 小时前
深入解析C++list:从0到1实现一个完整的链表类
c++·链表·list
王老师青少年编程13 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:[NOIP 2015 提高组] 子串
c++·字符串·csp·高频考点·子串·信奥赛