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

相关推荐
秋说1 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
谭林杰2 小时前
B树和B+树
数据结构·b树
卡卡卡卡罗特3 小时前
每日mysql
数据结构·算法
chao_7894 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
蜉蝣之翼❉4 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc
aramae4 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
lifallen5 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
lixzest5 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
不吃洋葱.5 小时前
前缀和|差分
数据结构·算法
_Chipen6 小时前
C++基础问题
开发语言·c++