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

相关推荐
t***5442 小时前
如何配置Orwell Dev-C++使用Clang
开发语言·c++
CoderCodingNo2 小时前
【信奥业余科普】C++ 的奇妙之旅 | 13:为什么 0.1+0.2≠0.3?——解密“爆int”溢出与浮点数精度的底层原理
开发语言·c++
小辉同志3 小时前
215. 数组中的第K个最大元素
数据结构·算法·leetcode··快速选择
极客智造4 小时前
深入详解 C++ 智能指针:RAII 原理、分类特性、底层机制与工程实战
c++·智能指针
王璐WL5 小时前
【C++】类的默认成员函数(上)
c++
王老师青少年编程5 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【区间贪心】:区间覆盖(加强版)
c++·算法·贪心·csp·信奥赛·区间贪心·区间覆盖(加强版)
宏笋6 小时前
C++11完美转发的作用和用法
c++
格发许可优化管理系统6 小时前
MathCAD许可类型全面解析:选择最适合您的许可证
c++
风一样的航哥6 小时前
LeetCode 2615 等值距离和:前缀和优化O(n)解法深度解析
数据结构·算法·leetcode
旖-旎6 小时前
深搜(二叉树的所有路径)(6)
c++·算法·leetcode·深度优先·递归