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

相关推荐
我是李武涯3 分钟前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
yuuki2332338 分钟前
【数据结构】用顺序表实现通讯录
c语言·数据结构·后端
卡提西亚40 分钟前
C++笔记-10-循环语句
c++·笔记·算法
还是码字踏实40 分钟前
基础数据结构之数组的双指针技巧之对撞指针(两端向中间):三数之和(LeetCode 15 中等题)
数据结构·算法·leetcode·双指针·对撞指针
亮剑20181 小时前
第1节:C语言初体验——环境、结构与基本数据类型
c++
William_wL_2 小时前
【C++】类和对象(下)
c++
William_wL_2 小时前
【C++】内存管理
c++
星星火柴9363 小时前
笔记 | C++面向对象高级开发
开发语言·c++·笔记·学习
悲伤小伞3 小时前
Linux_Socket_UDP
linux·服务器·网络·c++·网络协议·udp
八个程序员4 小时前
自定义函数(C++)
开发语言·c++·算法