当前位置:网站首页>力扣-二叉树的最大的深度
力扣-二叉树的最大的深度
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;
这种写法时间复杂度会非常大,因为它两次递归左子树和右子树。
边栏推荐
猜你喜欢

方法重写与Object类

迁移学习——Distant Domain Transfer Learning

Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
![[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective](/img/de/944b31c68cc5b9ffa6a585530e7be9.png)
[Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective

【Endnote】Word插入自定义形式的Endnote文献格式

Live preview | 30 minutes started quickly!Look at credible distributed AI chain oar architectural design

Transfer Learning - Joint Geometrical and Statistical Alignment for Visual Domain Adaptation

直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning

《.NET物联网从零开始》系列

IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
随机推荐
英特尔 XDC 2022 精彩回顾:共建开放生态,释放“基建”潜能
用@Mapper查询oracle的分区情况报错
.Net C# Console Create a window using Win32 API
亚马逊云科技携手中科创达为行业客户构建AIoT平台
EBS uses virtual columns and hint hints to optimize sql case
Three handshake and four wave in tcp
方法重写与Object类
ExcelPatternTool: Excel表格-数据库互导工具
HOG特征学习笔记
迁移学习——Distant Domain Transfer Learning
Transfer Learning - Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
js中try...catch和finally的用法
Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
DDOS攻击真的是无解吗?不!
How to create an rpm package
意识形态的机制
使用SuperMap iDesktopX数据迁移工具迁移ArcGIS数据
《.NET物联网从零开始》系列
.Net C# 控制台 使用 Win32 API 创建一个窗口