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

相关推荐
xu_yule19 分钟前
算法基础(数论)—费马小定理
c++·算法·裴蜀定理·欧拉定理·费马小定理·同余方程·扩展欧几里得定理
宇宙超级无敌暴龙战士4 小时前
旮旯c语言三个任务
c++·c
BanyeBirth5 小时前
C++差分数组(二维)
开发语言·c++·算法
Fcy6485 小时前
C++ map和multimap的使用
开发语言·c++·stl
CC.GG5 小时前
【C++】STL容器----unordered_map和unordered_set的使用
java·数据库·c++
lengjingzju6 小时前
基于IMake的 GCC 编译与链接选项深度解析:构建高效、安全、可调试的现代软件
c++·安全·性能优化·软件构建·开源软件
xu_yule7 小时前
算法基础(数论)—算法基本定理
c++·算法·算数基本定理
CoderCodingNo7 小时前
【GESP】C++五级真题(结构体排序考点) luogu-B3968 [GESP202403 五级] 成绩排序
开发语言·c++·算法
浅川.257 小时前
STL专项:stack 栈
数据结构·stl·stack
youngee119 小时前
hot100-56最小栈
数据结构