当前位置:网站首页>【 剑指 Offer】55 - I. 二叉树的深度
【 剑指 Offer】55 - I. 二叉树的深度
2022-07-01 13:26:00 【LuZhouShiLi】
剑指 Offer 55 - I. 二叉树的深度
题目
输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
思路
节点既有左子树又有右子树,那么树的深度就是左右子树深度的较大值再加1。
代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == nullptr)
{
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return (left > right) ? (left + 1) :(right + 1);
}
};
边栏推荐
- 研发效能度量框架解读
- 3.4 《数据库系统概论》之数据查询—SELECT(单表查询、连接查询、嵌套查询、集合查询、多表查询)
- Research Report on China's software outsourcing industry investment strategy and the 14th five year plan Ⓡ 2022 ~ 2028
- The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
- 8 popular recommended style layout
- Asp.netcore利用dynamic简化数据库访问
- French Data Protection Agency: using Google Analytics or violating gdpr
- 04-Redis源码数据结构之字典
- arthas使用
- 【241. 为运算表达式设计优先级】
猜你喜欢
![[241. Design priority for operation expression]](/img/72/29d27204d5213a8efdb2c5be925dec.png)
[241. Design priority for operation expression]

9. Use of better scroll and ref

洞态在某互联⽹⾦融科技企业的最佳落地实践

学历、长相、家境普通的人,未来的发展方向是什么?00后的职业规划都已经整得明明白白......

Simple two ball loading

Google Earth Engine(GEE)——全球人类居住区网格数据 1975-1990-2000-2014 (P2016)

La taille de la pile spécifiée est petite, spécifiée à la sortie 328k

IO的几种模型 阻塞,非阻塞,io多路复用,信号驱动和异步io

焱融看 | 混合云时代下,如何制定多云策略

Svg diamond style code
随机推荐
Asp. NETCORE uses dynamic to simplify database access
Analysis report on the development prospect and investment strategic planning of China's wafer manufacturing Ⓔ 2022 ~ 2028
[241. Design priority for operation expression]
Application of 5g industrial gateway in scientific and technological overload control; off-site joint law enforcement for over limit, overweight and overspeed
分布式事务简介(seata)
spark源码(五)DAGScheduler TaskScheduler如何配合提交任务,application、job、stage、taskset、task对应关系是什么?
Reasons for MySQL reporting 1040too many connections and Solutions
刘对(火线安全)-多云环境的风险发现
Leetcode第一题:两数之和(3种语言)
1553B环境搭建
流量管理技术
基于mysql乐观锁实现秒杀的示例代码
B站被骂上了热搜。。
ArrayList capacity expansion mechanism and thread safety
The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
Learning to use livedata and ViewModel will make it easier for you to write business
Sign APK with command line
一文读懂TDengine的窗口查询功能
Explain IO multiplexing, select, poll, epoll in detail
JS discolored Lego building blocks