当前位置:网站首页>LeetCode_二叉树_中等_515.在每个树行中找最大值
LeetCode_二叉树_中等_515.在每个树行中找最大值
2022-08-08 16:44:00 【小城老街】
1.题目
给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
示例1:
输入: root = [1,3,2,5,3,null,9]
输出: [1,3,9]
示例2:
输入: root = [1,2,3]
输出: [1,3]
提示:
二叉树的节点个数的范围是 [0,104]
-231 <= Node.val <= 231 - 1
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-largest-value-in-each-tree-row
2.思路
(1)层序遍历
既然要找出该二叉树中每一层的最大值,那么可以通过层序遍历的方式来进行寻找,在遍历每一层时,使用遍历 curMax 来保存当前层的最大值,在每一层遍历结束后,将 curMax 加入到 res 中即可,遍历结束后返回 res。具体的遍历细节可参考102.二叉树的层序遍历这篇文章。
(2)深度优先遍历 (DFS)
具体细节可看下面代码中的注释。
3.代码实现(Java)
//思路1————层序遍历
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
//queue保存层序遍历过程中每一层的所有节点
Queue<TreeNode> queue = new LinkedList<>();
//最开始的第一层只有根节点,故先将根节点加入到队列中
queue.offer(root);
// while 循环控制从上向下方向的遍历
while (!queue.isEmpty()) {
int levelSize = queue.size();
int curMax = Integer.MIN_VALUE;
// for 循环控制每一层从左向右的遍历
for (int i = 0; i < levelSize; i++) {
TreeNode curNode = queue.poll();
curMax = Math.max(curMax, curNode.val);
//将下一层中的所有节点保存到 queue 中
if (curNode.left != null) {
queue.offer(curNode.left);
}
if (curNode.right != null) {
queue.offer(curNode.right);
}
}
res.add(curMax);
}
return res;
}
}
//思路2————深度优先遍历 (DFS)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
// res 保存最终结果
List<Integer> res = new ArrayList<>();
public List<Integer> largestValues(TreeNode root) {
if (root == null) {
return res;
}
dfs(root, 0);
return res;
}
private void dfs(TreeNode root, int curHeight) {
/* res 中的下标表示二叉树中的层数(从 0 开始),下标对应的值表示该层中的最大值 */
if (curHeight == res.size()) {
/* 如果 curHeight == res.size(),那么则说明 res 还未保存当前第 curHeight 层 的最大值信息,所以直接将当前节点值加入到 res 中 */
res.add(root.val);
} else {
// 更新当前层的最大值
res.set(curHeight, Math.max(res.get(curHeight), root.val));
}
// 搜索左子树
if (root.left != null) {
dfs(root.left, curHeight + 1);
}
// 搜索右子树
if (root.right != null) {
dfs(root.right, curHeight + 1);
}
}
}
边栏推荐
- 数字图像处理(六)—— 图像压缩
- 2022年11大最佳缺陷管理工具盘点
- Solve the inexplicable problem of MySQL violently - restart the service!
- 【论文阅读】RAL 2022: Receding Moving Object Segmentation in 3D LiDAR Data Using Sparse 4D Convolutions
- QCon 回顾 | Data Fabric:逻辑统一、物理分散
- 使用 ansible-bender 构建容器镜像
- MySQL数据库的简介及select语句的执行流程
- 找工作的我看了国聘app
- C1. Pokémon Army (easy version)
- Building and Visualizing Sudoku Games with Pygame
猜你喜欢
随机推荐
淘宝API常用接口列表与申请方式
二、pytest+selenium+allure实现web ui自动化
Charles MOCK 数据 htpps代理
多线程-并发编程
bzoj1507 [NOI2003]Editor
GHOST tool to access the database
Obtain - 64 [chances] : the soldier, subtlety also - 5 - read sun tzu - melee meter
七、jmeter发出请求的逻辑
Grid 布局介绍
mysql 索引和 pgsql 索引 命名区别
Mysql都有那些最需要掌握的原理?
C. Palindromifier
ImportError: numpy.core.multiarray failed to import [cv2, matplotlib, PyTorch, pyinstaller ]
【LeetCode】试题总结:深度优先搜索 (DFS)
使用 Pygame 构建和可视化数独游戏
GHOST工具访问数据库
B. Stairs
赶紧进来修内功!!!带你认识C语言中各种进制数和原码反码补码.
vi编辑器命令
Nervegrowold: machine advanced learning advice








![Acwing Week 63 [Unfinished]](/img/ad/aee803fddbba688366ca8f3e772764.png)
