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

相关推荐
小龙报8 分钟前
【51单片机】不止是调光!51 单片机 PWM 实战:呼吸灯 + 直流电机正反转 + 转速控制
数据结构·c++·stm32·单片机·嵌入式硬件·物联网·51单片机
彩妙不是菜喵11 分钟前
C++:深入浅出讲解=>多态
开发语言·c++
qq_4542450341 分钟前
Graphkey:使用占位符彻底解耦函数与工作流
数据结构·c#
测绘工程师1 小时前
【排序算法】冒泡排序
数据结构·算法·排序算法
lightqjx1 小时前
【C++】C++11 - Lambda表达式+包装器
开发语言·c++·c++11·lambda·包装器
载数而行5201 小时前
算法系列1之最小生成树
c语言·数据结构·c++·算法·贪心算法
额,不知道写啥。1 小时前
HAO的DP
c++·算法·深度优先·动态规划
重生之后端学习1 小时前
208. 实现 Trie (前缀树)
java·开发语言·数据结构·算法·职场和发展·深度优先
我命由我123452 小时前
C++ EasyX 开发,MessageBox 函数参数问题:“const char *“ 类型的实参与 “LPCWSTR“ 类型的形参不兼容
c语言·开发语言·c++·后端·学习·visualstudio·visual studio
识君啊2 小时前
Java 栈 - 附LeetCode 经典题解
java·数据结构·leetcode·deque··stack·lifo