当前位置:网站首页>力扣-二叉树的最大的深度
力扣-二叉树的最大的深度
2022-08-05 02:00:00 【qq_52025208】
题目描述:给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
return leftHeight>rightHeight?leftHeight+1:rightHeight+1;
}
}
注意:
return maxDepth(root.left)>maxDepth(root.right)?maxDepth(root.left)+1:maxDepth(root.right)+1;
这种写法时间复杂度会非常大,因为它两次递归左子树和右子树。
边栏推荐
- 如何创建rpm包
- 方法重写与Object类
- C language basics -- pointers
- ExcelPatternTool: Excel table-database mutual import tool
- EBS uses virtual columns and hint hints to optimize sql case
- GC高德坐标和百度坐标转换
- 用@Mapper查询oracle的分区情况报错
- Greenplum数据库故障分析——能对数据库base文件夹进行软连接嘛?
- IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
- LPQ (local phase quantization) study notes
猜你喜欢

如何逐步执行数据风险评估

如何基于OpenVINO POT工具简单实现对模型的量化压缩

<开发>实用工具

直播预告|30分钟快速入门!来看可信分布式AI链桨的架构设计

CPDA|运营人如何从负基础学会数据分析(SQL)
![[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?](/img/eb/535ffaff9b535fbc73a4d56aab0b3a.png)
[How to smash wool according to the music the couple listens to during the Qixi Festival] Does the background music affect the couple's choice of wine?

1349. Maximum number of students taking the exam Status Compression

MySQL learning

ExcelPatternTool: Excel表格-数据库互导工具

The use of pytorch: temperature prediction using neural networks
随机推荐
[Endnote] Word inserts a custom form of Endnote document format
亚马逊云科技携手中科创达为行业客户构建AIoT平台
海量服务实例动态化管理
Programmer's list of sheep counting when insomnia | Daily anecdote
The difference between a process in user mode and kernel mode [exclusive analysis]
Is DDOS attack really unsolvable?Do not!
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
第十四天&postman
Tree search (bintree)
CMS website construction process
iNFTnews | 对体育行业和球迷来说,NFT可以带来什么?
dotnet 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
day14--postman interface test
软件测试技术之最有效的七大性能测试技术
转:查尔斯·汉迪:你是谁,比你做什么更重要
Simple implementation of YOLOv7 pre-training model deployment based on OpenVINO toolkit
C语言基础知识 -- 指针
1349. 参加考试的最大学生数 状态压缩