当前位置:网站首页>力扣 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;
}
}
边栏推荐
- Li Kou 79 word search
- Prism framework project application - Navigation
- XML parsing bean tool class
- MapReduce execution principle record
- 【MySQL】 MySQL 导出数据库
- Binary search method
- Open camera anomaly analysis (I)
- Slide the menu of uni app custom components left and right and click switch to select and display in the middle
- 816. 模糊坐标
- Comparison of static methods and variables with instance methods and variables
猜你喜欢
【Flink】Flink Sort-Shuffle写流程简析
matplotlib折线图,文字显示,win10
Open camera anomaly analysis (I)
ABP framework Practice Series (II) - Introduction to domain layer
MySQL advanced part (IV: locking mechanism and SQL optimization)
Analysis of camera memory memory leakage (II)
YOLOv5改进:更换骨干网(Backbone)
Intelligent manufacturing learning videos and books
After Ali failed to start his job in the interview, he was roast by the interviewer in the circle of friends (plug)
Camera memory memory leak analysis (III)
随机推荐
Some mobile phones open USB debugging, and the solution to installation failure
ASP. Net startup and running mechanism
Webrtc series - 7-ice supplement of network transmission preference and priority
Analysis of camera memory memory leakage (II)
EF core Basics
Optimization - multi objective planning
User control custom DependencyProperty
169. 多数元素
Uni app custom drop-down selection list
Camera memory memory leak analysis (III)
How to solve the problem that iterative semi supervised training is difficult to implement in ASR training? RTC dev Meetup
[Flink] a brief analysis of the writing process of Flink sort shuffle
763. dividing alphabetic intervals
WebRTC系列-网络传输之6-Connections裁剪
MySQL common statements
High performance computing center roce overview
Android gap animation translate, scale, alpha, rotate
2022.6.25 - leetcode. Un doigt d'épée. 091.
Uni app Baidu cloud realizes OCR ID card recognition
【MySQL】 MySQL 导出数据库