当前位置:网站首页>力扣 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;
}
}

边栏推荐
- Custom parameter QR code picture combined with background picture to generate new picture node environment
- 三元损失英文版
- Android gap animation translate, scale, alpha, rotate
- High performance computing center roce overview
- MySQL advanced part (IV: locking mechanism and SQL optimization)
- Pay attention to the entrance components of official account in the applet
- 2022.6.25-----leetcode. Sword finger offer 091
- Analysis of camera memory memory leakage (II)
- 169. 多数元素
- [LOJ 6718] nine suns' weakened version (cyclic convolution, arbitrary modulus NTT)
猜你喜欢

Oracle technology sharing Oracle 19.14 upgrade 19.15

如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup

【Flink】Flink 批处理模式Map端数据聚合 NormalizedKeySorter

软件调试测试的十大重要基本准则

Analysis of camera memory memory leakage (II)

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

High performance computing center roce overview

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

Camera-memory内存泄漏分析(三)

【LOJ#6718】九个太阳「弱」化版(循环卷积,任意模数NTT)
随机推荐
Go time package: second, millisecond, nanosecond timestamp output
Binary search method
An error occurred using the connection to database 'on server' 10.28.253.2‘
优化——多目标规划
Procédures stockées MySQL
Prism framework
MySQL高級篇第一章(linux下安裝MySQL)【下】
WebRTC系列-网络传输之7-ICE补充之偏好(preference)与优先级(priority)
1. foundation closing
JS to achieve the effect of text marquee
【好书集锦】从技术到产品
2022.6.20-----leetcode. seven hundred and fifteen
Kotlin learning apply plugin: 'kotlin Android extensions‘
View of MySQL
开源!ViTAE模型再刷世界第一:COCO人体姿态估计新模型取得最高精度81.1AP
2022.6.25-----leetcode.剑指offer.091
The style of the mall can also change a lot. DIY can learn about it
智能制造学习记录片和书籍
QPS的概念和实现
Restful API interface design standards and specifications