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

相关推荐
xlq223222 小时前
22.多态(上)
开发语言·c++·算法
D_evil__2 小时前
[C++高频精进] 并发编程:线程基础
c++
云里雾里!3 小时前
力扣 209. 长度最小的子数组:滑动窗口解法完整解析
数据结构·算法·leetcode
Mr_WangAndy3 小时前
C++17 新特性_第二章 C++17 语言特性_std::any和string_view
c++·string_view·c++40周年·c++17新特性·c++新特性any
憨憨崽&4 小时前
进击大厂:程序员必须修炼的算法“内功”与思维体系
开发语言·数据结构·算法·链表·贪心算法·线性回归·动态规划
水天需0105 小时前
C++ 三种指针转换深度解析
c++
chem41115 小时前
C 语言 函数指针和函数指针数组
c语言·数据结构·算法
言言的底层世界5 小时前
c++中STL容器及算法等
开发语言·c++·经验分享·笔记
Mr_WangAndy6 小时前
C++17 新特性_第一章 C++17 语言特性___has_include,u8字符字面量
c++·c++40周年·c++17新特性·__has_include·u8字面量
liu****6 小时前
八.函数递归
c语言·开发语言·数据结构·c++·算法