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

相关推荐
apcipot_rain29 分钟前
C++开发 面试 1Day速通
开发语言·c++·面试
星恒随风1 小时前
从0实现C++ AI大模型接入SDK(七):Ollama本地模型部署、NDJSON流式响应与LLMManager模型管理
linux·开发语言·c++·人工智能·笔记
老师我太想进步了20261 小时前
【数据结构-15】实现链式结构二叉树
c语言·数据结构
xiangxueCode4 小时前
数据结构:容器适配器之优先级队列
数据结构·优先级队列
鱼很腾apoc5 小时前
【Redis】第2期 详解Redis五大数据结构:命令+C++实现、渐进式遍历与数据库管理
linux·服务器·c语言·数据结构·c++·redis·学习
郝学胜-神的一滴5 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
bkspiderx16 小时前
C++符号冲突与内部符号优先策略详解
linux·服务器·c++·符号冲突·内部符号
ShineWinsu18 小时前
对于Qt:条件变量(QWaitCondition)的解析
c++·qt·面试·线程·条件变量·客户端·锁
微笑星河q19 小时前
C++高阶之【多态】
开发语言·c++
青少儿编程课堂1 天前
第十八届蓝桥杯青少组图形化真题:校园分形雪花绘制解析(全国青少年信息素养大赛·电子学会图形化考级冲刺)
c++·python·算法·bfs·信息学竞赛