当前位置:网站首页>Leetcode daily question - 515 Find the maximum value in each tree row
Leetcode daily question - 515 Find the maximum value in each tree row
2022-06-25 07:37:00 【SK_ Jaco】
1. Title Description
515. Find the maximum in each tree row
Given the root node of a binary tree root , Please find the maximum value of each layer in the binary tree .
Example 1:
Input : root = [1,3,2,5,3,null,9]
Output : [1,3,9]
Example 2:
Input : root = [1,2,3]
Output : [1,3]
2. Problem solving ideas and codes
2.1 Their thinking
The topic requires to obtain the maximum value of each layer of the binary tree , Then you can use the sequence traversal of binary tree , Here you can also use the binary tree sequence traversal feature : When the number of times put in the queue is equal to the length of the queue , Then the next level has been put into the queue . When placing left and right nodes , Calculate the maximum , After each layer is traversed, the maximum value is put into the result list , At the same time, reset the maximum value and put times .
2.2 Code
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if (root == null){
return ans;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int count = 1;
int max = root.val;
while (!queue.isEmpty()) {
if (count == queue.size()) {
// After traversing a layer, put the maximum value of this layer into the result list , And initialize the maximum value and put times
ans.add(max);
max = Integer.MIN_VALUE;
count = 0;
}
TreeNode poll = queue.poll();
// When placing left and right nodes , Calculate the maximum
if (poll.left != null) {
max = Math.max(max, poll.left.val);
queue.offer(poll.left);
count++;
}
if (poll.right != null) {
max = Math.max(max, poll.right.val);
queue.offer(poll.right);
count++;
}
}
return ans;
}
}
2.3 test result
Pass the test 
3. summary
- Use binary tree sequence traversal
- After each layer is traversed, record the maximum value of this layer
边栏推荐
- The perfect presentation of Dao in the metauniverse, and platofarm creates a farm themed metauniverse
- 全局变量&局部变量
- 13 `bs_duixiang.tag标签`得到一个tag对象
- 正版photoshop2022购买体验经历分享
- 几款不错的天气插件
- [batch dos-cmd command - summary and summary] - commands related to Internet access and network communication (Ping, Telnet, NSLOOKUP, ARP, tracert, ipconfig)
- Explain distributed raft with dynamic diagram
- 三年营收连续下滑,天地壹号困在醋饮料里
- 14 BS object Node name Name attrs string get node name attribute content
- Debug through yalc before releasing NPM package
猜你喜欢

What is the difference between norflash and nandflash

Sqlmap advanced use – cookies

14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容

This year, I graduated

Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w
![对链表进行插入排序[dummy统一操作+断链核心--被动节点]](/img/2a/ccb1145d2b4f9fbd8d0812deace93b.png)
对链表进行插入排序[dummy统一操作+断链核心--被动节点]

太上老君的炼丹炉之分布式 Quorum NWR

What is the new business model of Taishan crowdfunding in 2022?

GUI pull-down menu of unity3d evil door implementation dropdown design has no duplicate items

Chuantu microelectronics high speed and high performance rs-485/422 transceiver series
随机推荐
Redirect to previous page after login? PHP - Redirecting to previous page after login? PHP
Hanxin's trick: consistent hashing
高考志愿填报,为啥专业最后考虑?
Global variables & local variables
Ca-is1200u current detection isolation amplifier has been delivered in batch
Sichuan Tuwei ca-is3105w fully integrated DC-DC converter
VectorDraw Developer Framework 10.10
Display purchase Summary - Dell 2705qm BenQ pd2700u
Redis learning notes
Debian introduction
正版photoshop2022購買體驗經曆分享
PI Ziheng embedded: This paper introduces the multi-channel link mode of i.mxrt timer pit and its application in coremark Test Engineering
Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection
TEMPEST HDMI泄漏接收 2
【批处理DOS-CMD命令-汇总和小结】-添加注释命令(rem或::)
Operate cnblogs metaweblog API
Construction of occupancy grid map
栅格地图(occupancy grid map)构建
Tuwei Digital Isolator and interface chip can perfectly replace imported brands Ti and ADI
【批处理DOS-CMD命令-汇总和小结】-文件与目录操作命令(md、rd、xcopy、dir、cd、set、move、copy、del、type、sort)