当前位置:网站首页>力扣 515. 在每个树行中找最大值
力扣 515. 在每个树行中找最大值
2022-06-26 03:53:00 【冷酷的摸鱼小将】
题目
给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
示例

输入: root = [1,3,2,5,3,null,9]
输出: [1,3,9]
输入: root = [1,2,3]
输出: [1,3]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-largest-value-in-each-tree-row
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
方法1:队列
Java实现
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
int sz = q.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < sz; i++) {
TreeNode cur = q.poll();
max = cur.val > max ? cur.val : max;
if (cur.left != null) q.offer(cur.left);
if (cur.right != null) q.offer(cur.right);
}
res.add(max);
}
return res;
}
}

边栏推荐
- High performance computing center roce overview
- [MySQL] MySQL export database
- 三元损失英文版
- 阿里云函数计算服务一键搭建Z-Blog个人博客
- Procédures stockées MySQL
- 【MySQL】 MySQL 导出数据库
- Webrtc series - 7-ice supplement of network transmission preference and priority
- 2022.6.23-----leetcode.30
- 2022.6.25 - leetcode. Un doigt d'épée. 091.
- 2022.6.23-----leetcode. thirty
猜你喜欢

Flask入门

Uni app, the text implementation expands and retracts the full text

ABP framework Practice Series (II) - Introduction to domain layer

Open source! Vitae model brushes the world's first again: the new coco human posture estimation model achieves the highest accuracy of 81.1ap

Comparison of static methods and variables with instance methods and variables
![[Flink] a brief analysis of the writing process of Flink sort shuffle](/img/27/01e95b44df46d8bfcb36ab1d746cc2.jpg)
[Flink] a brief analysis of the writing process of Flink sort shuffle

You cannot call Glide. get() in registerComponents(), use the provided Glide instance instead

The style of the mall can also change a lot. DIY can learn about it

The kotlin project is running normally and the R file cannot be found

判断两个集合的相同值 ||不同值
随机推荐
Camera-CreateCaptureSession
Open Camera异常分析(一)
"Renegotiation" agreement
商城风格也可以很多变,DIY 了解一下
MySQL高級篇第一章(linux下安裝MySQL)【下】
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
Camera memory memory leak analysis (III)
Webrtc series - 7-ice supplement of network transmission preference and priority
【好书集锦】从技术到产品
Kotlin uses viewpager2+fragment+bottomnavigationview to implement the style of the switching module of the bottom menu bar.
软件调试测试的十大重要基本准则
神经网络学习小记录71——Tensorflow2 使用Google Colab进行深度学习
优化——多目标规划
【Flink】Flink源码分析——批处理模式JobGraph的创建
matplotlib多条折线图,点散图
机器学习笔记 - 时间序列的趋势分量
Oracle technology sharing Oracle 19.14 upgrade 19.15
Some mobile phones open USB debugging, and the solution to installation failure
MapReduce execution principle record
WebRTC系列-网络传输之7-ICE补充之偏好(preference)与优先级(priority)