当前位置:网站首页>LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
2022-08-11 03:42:00 【小机double】
LeetCode 104 二叉树的最大深度
题目描述
给定一个二叉树,找出其最大深度
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例
给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最大深度 3 。
题目解析
对于学过数据结构的可能这道题很快就能使用递归的方式写出来了。树的深度也就是树的高度,计算的时候一般可以使用深度优先搜索的方式递归遍历树结构,当然使用递归的方式也能够使用栈的方式进行实现。下面是两种实现方式:
使用递归
对于任意一个节点,如果其左右子树为空,则表明该树只有一个节点,返回1。否则,递归比较左右子树的大小,去最大值再加上1(表示该树的根节点),代码如下:
public int maxDepth(TreeNode root) { if (root == null) { return 0; } if (root.left == null && root.right == null) { return 1; } return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; //比较左右子树深度的最大值再加上该根节点 }程序运行结果:

使用栈结构的非递归方法:
对于非递归的方法,使用队列也是可以的,队列对应的思想是广度优先搜索,或者说是树的层次遍历,而深度优先搜索改成非递归的方式就要采用栈结构了,毕竟递归本身也是使用系统栈结构来实现的。
具体思路如下:
- 创建一个空栈,将树根进栈;
- 栈不为空的时候循环遍历栈,每次查看(注意不是弹出)栈顶指针指向的元素,如果栈顶节点的左右子树为空,则将该栈顶出栈(相对应于递归的时候返回改成递归操作),树的深度-1,继续循环;
- 如果栈顶节点存在左子树,则将左子树入栈,并且将栈顶指针的左孩子置为空,避免下次又重新将该左子树进栈。树的深度加1,继续循环遍历;
- 如果栈顶节点存在右子树,则将右子树入栈,并且将栈顶指针的右孩子置空,树的深度+1;继续循环遍历。
由于在实现的时候树的深度不能够多次累加,所以设置了一个最大的深度来记录当前遍历到的树的最大深度。以示例进行图示说明:

代码如下:
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
int deep = 1;
int maxDeep = 1;
while (!stack.empty()) {
TreeNode peek = stack.peek(); //peek为查看栈顶指针
if (peek.left == null && peek.right == null) {
//叶子节点将栈顶出栈
deep--;
stack.pop();
continue;
}
if (peek.left != null) {
//先查看左子树,就跟递归遍历左子树一样
stack.add(peek.left);
peek.left = null;
deep++;
if (deep > maxDeep) {
//有更大的深度则更改最大深度的值
maxDeep++;
}
continue;
}
if (peek.right != null) {
stack.add(peek.right);
peek.right = null;
deep++;
if (deep > maxDeep) {
maxDeep++;
}
}
}
return maxDeep;
}
然而。。。程序运行的好像不是那么理想。。。

边栏推荐
- 程序化交易改变了什么?
- Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
- Qnet Weak Network Test Tool Operation Guide
- 程序化交易的策略类型可以分为哪几种?
- LeetCode Hot Questions (12. The Best Time to Buy and Sell Stocks)
- Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method
- 互换性与测量技术——表面粗糙度选取和标注方法
- MYSQLg advanced ------ clustered and non-clustered indexes
- Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
- 输入起始位置,终止位置截取链表
猜你喜欢

Build Zabbix Kubernetes cluster monitoring platform

2022-08-10 The sixth group Hiding spring study notes

机器学习中什么是集成学习?

多串口RS485工业网关BL110

Description of ESB product development steps under cloud platform

没想到MySQL还会问这些...

E-commerce project - mall time-limited seckill function system

机器学习可以应用在哪些场景?机器学习有什么用?

【FPGA】day19-二进制转换为十进制(BCD码)

Interchangeable Measurement Techniques - Geometric Errors
随机推荐
互换性测量技术-几何误差
【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
【FPGA】名词缩写
A brief analysis of whether programmatic futures trading or manual order is better?
CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
构建程序化交易系统需要注意什么问题?
[FPGA] day19- binary to decimal (BCD code)
What has programmatic trading changed?
Day20 FPGA 】 【 - block the I2C read and write EEPROM
云平台下ESB产品开发步骤说明
“顶梁柱”滑坡、新增长极难担重任,阿里“蹲下”是为了跳更高?
什么是机器强化学习?原理是什么?
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
What kind of programming trading strategy types can be divided into?
作业8.10 TFTP协议 下载功能
Homework 8.10 TFTP protocol download function
【愚公系列】2022年08月 Go教学课程 035-接口和继承和转换与空接口
【FPGA】day20-I2C读写EEPROM
什么是三方支付?
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes