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

相关推荐
earthzhang202125 分钟前
第3讲:Go垃圾回收机制与性能优化
开发语言·jvm·数据结构·后端·性能优化·golang
AA陈超29 分钟前
虚幻引擎5 GAS开发俯视角RPG游戏 P05-08 UI 部件数据表
c++·游戏·ue5·游戏引擎·虚幻
纵有疾風起1 小时前
C++——类和对象(3)
开发语言·c++·经验分享·开源
承渊政道3 小时前
动态内存管理
c语言·c++·经验分享·c#·visual studio
best_virtuoso3 小时前
PostgreSQL 常见数组操作函数语法、功能
java·数据结构·postgresql
孤独得猿3 小时前
聊天室项目开发——etcd的安装和使用
linux·服务器·c++·etcd
new coder3 小时前
[c++语法学习]Day10:c++引用
开发语言·c++·学习
无敌最俊朗@4 小时前
数组-力扣hot56-合并区间
数据结构·算法·leetcode
哼?~4 小时前
C++11标准 上 (万字解析)
开发语言·c++
码农多耕地呗4 小时前
力扣94.二叉树的中序遍历(递归and迭代法)(java)
数据结构·算法·leetcode