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

相关推荐
齐落山大勇17 小时前
数据结构——栈与队列
数据结构
毅炼17 小时前
hot100打卡——day17
java·数据结构·算法·leetcode·深度优先
404未精通的狗17 小时前
(数据结构)二叉树(上)
数据结构
Trouvaille ~17 小时前
【Linux】线程同步与互斥(三):生产者消费者模型实战
linux·运维·c++·信号量·阻塞队列·生产者消费者模型·环形队列
Queenie_Charlie17 小时前
八皇后问题
c++·深度优先搜索
m0_7369191017 小时前
编译器命令选项优化
开发语言·c++·算法
Jiu-yuan18 小时前
C++函数
c++
naruto_lnq18 小时前
C++中的工厂方法模式
开发语言·c++·算法
一切尽在,你来18 小时前
C++多线程教程-1.2.3 C++并发编程的平台无关性
开发语言·c++
寄存器漫游者18 小时前
数据结构 C语言 顺序栈
java·c语言·数据结构