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砸昏头5 分钟前
C++与Node.js集成
开发语言·c++·算法
程序员zgh10 分钟前
C++ 纯虚函数 — 抽象接口
c语言·开发语言·c++·经验分享·笔记·接口隔离原则
wen__xvn31 分钟前
码蹄杯刷题
数据结构·c++·算法
Remember_99335 分钟前
【数据结构】Java对象比较全解析:从equals到Comparable与Comparator,再到PriorityQueue应用
java·开发语言·数据结构·算法·leetcode·哈希算法
郝学胜-神的一滴37 分钟前
深入浅出网络协议:从OSI七层到TCP/IP五层模型全解析
开发语言·网络·c++·网络协议·tcp/ip·程序人生
夏乌_Wx40 分钟前
练题100天——DAY39:单链表练习题×5
c语言·数据结构·算法·链表
txinyu的博客1 小时前
布隆过滤器
数据结构·算法·哈希算法
jojo_zjx1 小时前
GESP 25年12月1级 手机电量显示
c++
程序员zgh1 小时前
C语言 弱定义机制 解读
c语言·开发语言·c++
宵时待雨1 小时前
数据结构(初阶)笔记归纳6:双向链表的实现
c语言·开发语言·数据结构·笔记·算法·链表