当前位置:网站首页>Force buckle 515 Find the maximum value in each tree row
Force buckle 515 Find the maximum value in each tree row
2022-06-26 03:59:00 【A ruthless young Fisherman】
subject
Given the root node of a binary tree root , Please find the maximum value of each layer in the binary tree .
Example

Input : root = [1,3,2,5,3,null,9]
Output : [1,3,9]
Input : root = [1,2,3]
Output : [1,3]
source : Power button (LeetCode)
link :https://leetcode.cn/problems/find-largest-value-in-each-tree-row
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: queue
Java Realization
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;
}
}

边栏推荐
- MySQL common statements
- Chrome page recording and playback function
- Judge the same value of two sets 𞓜 different values
- English version of ternary loss
- 力扣 515. 在每个树行中找最大值
- ipvs之ipvs0网卡
- (15)Blender源码分析之闪屏窗口显示菜单功能
- 线程同步之条件变量
- I/o virtualization technology - vfio
- The stc-isp burning program for 51 single chip microcomputer always shows that "the target single chip microcomputer is being detected..." the cold start board does not respond
猜你喜欢

Small record of neural network learning 71 - tensorflow2 deep learning with Google Lab

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

使用SOAPUI访问对应的esb工程

What preparation should I make before learning SCM?

机器学习笔记 - 时间序列的趋势分量

Can string be changed?

线程同步之读写锁

YOLOv5改进:更换骨干网(Backbone)

The stc-isp burning program for 51 single chip microcomputer always shows that "the target single chip microcomputer is being detected..." the cold start board does not respond

Part 4: drawing quadrilateral
随机推荐
169. most elements
Sorting out the knowledge points of the renderview renderobject parentdata of the shuttle
1. foundation closing
Ten important basic principles of software debugging and testing
Conditional variables for thread synchronization
Camera-memory内存泄漏分析(二)
Matplotlib line chart, text display, win10
User control custom DependencyProperty
阿里云函数计算服务一键搭建Z-Blog个人博客
[Flink] Flink batch mode map side data aggregation normalizedkeysorter
Open source! Vitae model brushes the world's first again: the new coco human posture estimation model achieves the highest accuracy of 81.1ap
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
ASP. Net startup and running mechanism
高性能算力中心 — RoCE — Overview
How to use EEPROM in 51 Single Chip Microcomputer?
I/o virtualization technology - UIO framework
R language and machine learning
2022.6.23-----leetcode. thirty
Webrtc series - 7-ice supplement of network transmission preference and priority
軟件調試測試的十大重要基本准則