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 小时前
笔试面试中关于链表相关的题目
数据结构·链表·面试·职场和发展
赵财猫._.2 小时前
Native API开发:C++与ArkTS混合编程实战
开发语言·c++·harmonyos
普通网友3 小时前
基于C++的操作系统开发
开发语言·c++·算法
2501_941111344 小时前
C++中的策略模式高级应用
开发语言·c++·算法
普通网友6 小时前
C++与Qt图形开发
开发语言·c++·算法
AA陈超6 小时前
UE5笔记:GetWorld()->SpawnActorDeferred()
c++·笔记·学习·ue5·虚幻引擎
普通网友6 小时前
C++中的适配器模式
开发语言·c++·算法
无敌最俊朗@6 小时前
力扣hot100-160-相交链表
c++
普通网友7 小时前
C++中的委托构造函数
开发语言·c++·算法
普通网友7 小时前
C++中的代理模式实战
开发语言·c++·算法