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

相关推荐
careathers5 小时前
【数据结构】从堆到 PriorityQueue:手写一个优先级队列
数据结构
y1su6 小时前
Leetcode 字符串模板
java·数据结构·算法·leetcode·职场和发展·动态规划
syagain_zsx7 小时前
BRpc 二次封装:Channel 管理、服务管理与回调工厂
c++·学习·程序人生·rpc·谷歌
fpcc8 小时前
计算机原理—如何高效的操作文件
c++·计算机原理
Rabitebla8 小时前
【系统性学习笔记】Linux 权限篇(二):文件属性、权限修改与目录权限的底层逻辑
linux·c++·笔记·学习·算法
system_error_x9 小时前
C++ 编程基础入门教程
开发语言·c++
布莱克60511 小时前
A* 算法详解:从原理到实现
开发语言·c++·算法
多弗朗皮卡丘11 小时前
C++ 智能指针
开发语言·c++·c++11·智能指针
进化中的刀盾兽11 小时前
从零实现 C++ AI 大模型接入 SDK(十):SessionManager 会话管理与多轮对话实现
c++
小成2023032026511 小时前
C++~02运算符重载
开发语言·c++