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

相关推荐
Trouvaille ~16 小时前
【C++篇】C++11新特性详解(一):基础特性与类的增强
c++·stl·c++11·类和对象·语法·默认成员函数·初始化列表
HUST16 小时前
C 语言 第九讲:函数递归
c语言·开发语言·数据结构·算法·c#
yaoh.wang16 小时前
力扣(LeetCode) 119: 杨辉三角 II - 解法思路
数据结构·python·算法·leetcode·面试·职场和发展·跳槽
客梦16 小时前
数据结构--最小生成树
数据结构·笔记
CSDN_RTKLIB16 小时前
【类定义系列一】C++ 头文件 / 源文件分离
开发语言·c++
CoderCodingNo16 小时前
【GESP】C++五级真题(埃氏筛思想考点) luogu-B3929 [GESP202312 五级] 小杨的幸运数
数据结构·c++·算法
charlee4416 小时前
C++中JSON序列化和反序列化的实现
c++·json·序列化·结构体·nlohmann/json
bbq粉刷匠16 小时前
Java--二叉树概念及其基础应用
java·数据结构·算法
挖矿大亨16 小时前
c++中值传递时是如何触发拷贝构造函数的
开发语言·c++
Cowboy hat16 小时前
数据结构基础(二):线性数据结构
数据结构