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

相关推荐
林开落L1 小时前
从入门到了解:Protobuf、JSON、XML 核心解析(C++ 示例)
xml·c++·json·protobuffer·结构化数据序列化机制
Queenie_Charlie1 小时前
stars(树状数组)
数据结构·c++·树状数组
会周易的程序员2 小时前
openplc runtimev4 Docker 部署
运维·c++·物联网·docker·容器·软件工程·iot
静听山水2 小时前
Redis核心数据结构-Set
数据结构·数据库·redis
爱装代码的小瓶子2 小时前
【C++与Linux基础】进程间通讯方式:匿名管道
android·c++·后端
CoderCodingNo2 小时前
【GESP】C++ 二级真题解析,[2025年12月]第一题环保能量球
开发语言·c++·算法
独好紫罗兰2 小时前
对python的再认识-基于数据结构进行-a005-元组-CRUD
开发语言·数据结构·python
LYOBOYI1232 小时前
qtcpSocket详解
c++·qt
REDcker2 小时前
gRPC完整文档
服务器·网络·c++·网络协议·grpc
wengqidaifeng2 小时前
数据结构(三)栈和队列(上)栈:计算机世界的“叠叠乐”
c语言·数据结构·数据库·链表