当前位置:网站首页>LeetCode腾讯精选练习50题-104.二叉树的最大深度
LeetCode腾讯精选练习50题-104.二叉树的最大深度
2022-04-22 11:38:00 【whtli】
题目描述
- 给定一个二叉树,找出其最大深度。
- 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
example
input : [3,9,20,null,null,15,7]
output : 3
note : 该二叉树的最大深度为 3
3
/ \
9 20
/ \
15 7
解题思路
思路1 递归
- 从下向上,每次返回的都是当前节点的具备最大深度的子树的深度,那么最终返回的就是整个二叉树的最大深度
- 递归退出条件:当前节点是空节点,返回0,即往下没有深度了
- 不为空,则分别递归左子树和右子树,求其最大深度
- 比较左右子树的最大深度并返回其中较大者
思路2 DFS
- 深度优先搜索
- 借助栈,实现非递归形式的深度优先搜索,先序、中序、后续都可以
- 在遍历过程中记录当前深度,并在切换左右子树的时候通过比较获取当前最大深度
- 不断更新最大深度,遍历结束后就可以获得整个二叉树的最大深度
思路3 BFS
- 广度优先搜索
- 借助队列,实现非递归形式的广度优先搜索
- 在遍历过程中,首先获取队列的长度,在这个长度内的节点,就是在同一层的
- 每次遍历完一层,就可以让深度加1,最终就可以获得最大深度
代码(Java)
思路1代码
public class Solution1 {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return loop(root);
}
public int loop(TreeNode node) {
if (node == null) {
return 0;
}
int left = 1 + loop(node.left);
int right = 1 + loop(node.right);
return Math.max(left, right);
}
}
思路2代码
public class Solution2 {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int curDepth = 0;
int maxDepth = 0;
Stack<Integer> allDepth = new Stack<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
// DFS
while (node != null || !stack.empty()) {
curDepth++;
while (node != null) {
//System.out.println(node.val);
stack.push(node);
allDepth.push(curDepth);
curDepth++;
node = node.left;
}
if (!stack.empty()) {
TreeNode tmp = stack.pop();
curDepth = allDepth.pop();
if (curDepth > maxDepth) {
maxDepth = curDepth;
}
node = tmp.right;
}
}
return maxDepth;
}
}
思路3代码
public class Solution3 {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int maxDepth = 0;
Queue<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
// BFS
while (!queue.isEmpty()) {
int size = queue.size();
while (size > 0) {
TreeNode tmp = queue.poll();
size --;
if (tmp.left != null) {
queue.add(tmp.left);
}
if (tmp.right != null) {
queue.add(tmp.right);
}
}
maxDepth ++;
}
return maxDepth;
}
}
版权声明
本文为[whtli]所创,转载请带上原文链接,感谢
https://blog.csdn.net/flower_48237/article/details/124296187
边栏推荐
- [untitled]
- L2-030 冰岛人 (25 分)
- ES6 learning notes 4 numerical representation related to numerical expansion (octal representation) (reprinted, the memo is not my original)
- 云融科技加入龙蜥社区,助力金融行业数字化转型
- Why can't people see the truth?
- 将博客搬至CSDN
- 2019-8-8-wpf - touch and mouse click response in non customer area
- 12. (map data) cesium city building map
- 基于 TiDB 的 Apache APISIX 高可用配置中心的最佳实践
- 阿里实习offer成功上岸,这几点至关重要
猜你喜欢

POSTGRESQL 15's new function is worth looking forward to, two of which make complaints about it for a long time.
![[security construction] Sysmon, the best tool for log monitoring](/img/24/4cb8565b51423e5756b292ca522a23.png)
[security construction] Sysmon, the best tool for log monitoring

Looking for investors of state-owned enterprises, central enterprises and listed companies, I choose Tami dog!

机器学习基础

全新威马M7同时斩获“iF设计奖”、“红点产品设计大奖”两项国际大奖

MySQL查看建库建表语句

2. flddler响应显示乱码问题解决方案

【安全建设】日志监控的极品工具sysmon

APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)

看的懂的C语言--字符串、转义字符、注释
随机推荐
.env.dev不生效问题
卷积神经网络
Intelligent party building integrated management platform development, digital party building integrated management system
深度迁移学习
全新威马M7同时斩获“iF设计奖”、“红点产品设计大奖”两项国际大奖
Convolutional neural network
基于泰凌微TLSR825x的物联网解决方案之ibeacon开发总结
200. 岛屿数量
Chat chat app Lesson 6 creating a main chat static page
Redis 解决键冲突
10.(地图数据篇)离线地形数据处理(供Cesium使用)
无刷直流电机矢量控制(一):概念和流程梳理
Scroll bar style modification
C语言小项目----> 推箱子
Redis environment installation
360数科等入选信通院首批数据安全管理能力认证公司
机器学习基础
Oracle 手工执行跨平台传输表空间(XTTS)
Redis (I) installation, configuration and startup of redis under Windows 10
After installing yarn correctly, use yarn installation in vscode and report an error