当前位置:网站首页>515. find the maximum value in each tree row
515. find the maximum value in each tree row
2022-06-26 19:52:00 【North_ dust】
Follow the last article , One more time leetcode My daily question , Topic link :515. Find the maximum in each tree row
The title is as follows :
Given the root node of a binary tree root , Please find... In the binary tree Each layer Of Maximum .
This problem uses the sequence traversal routine of binary tree , The code is as follows :
public static List<Integer> largestValues(TreeNode root) {
List<Integer> maxList = new ArrayList<>();
if(root == null){
return maxList;
}
LinkedList<TreeNode> list = new LinkedList<>();
list.addFirst(root);
while(!list.isEmpty()){
int size = list.size();
// Here is a brief note
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode treeNode = list.removeFirst();
if(treeNode.left != null){
list.addLast(treeNode.left );
}
if(treeNode.right != null){
list.addLast(treeNode.right);
}
int val = treeNode.val;
max = max > val ? max : val;
}
maxList.add(max);
}
return maxList;
}
边栏推荐
- 手机影像内卷几时休?
- 微服务版单点登陆系统(SSO)
- [recommended collection] these 8 common missing value filling skills must be mastered
- MySQL - database creation and management
- Tiktok practice ~ search page ~ video details
- Feitian +cipu body brings more imagination to the metauniverse
- Introduction to single chip microcomputer one-on-one learning strategy, independent development program immediately after reading
- C exercise. Class list plus records, display records and clear records
- Wechat applet custom pop-up components
- 项目实战四:用户登录及token访问验证(reids+jwt)
猜你喜欢

Preliminary analysis of serial port printing and stack for arm bare board debugging

Selection of database paradigm and main code

关于Qt数据库开发的一些冷知识

Development of NFT for digital collection platform

商品秒杀系统

Tiktok practice ~ sharing module ~ generate short video QR code

When does the mobile phone video roll off?

抖音实战~搜索页面~视频详情

项目实战六:分布式事务-Seata

Current limiting design and Implementation
随机推荐
ImageView, glide load long picture (glide load picture)
(几何) 凸包问题
超分之VRT
好物推荐:移动端开发安全工具
MySQL - subquery usage
String string is converted to jsonarray and parsed
飞天+CIPU体为元宇宙带来更大想象空间
关于不等式取值转义的思路
Feitian +cipu body brings more imagination to the metauniverse
Three basic backup methods of mongodb
回溯思路详解
Tiktok practice ~ sharing module ~ generate short video QR code
Wechat applet custom pop-up components
[kubernetes] kubernetes principle analysis and practical application (under update)
Boot指标监测
Uni app uses canvas to draw QR code
stm32和电机开发(直流有刷电机和步进电机)
Preliminary analysis of serial port printing and stack for arm bare board debugging
Project practice 4: user login and token access verification (reids+jwt)
Feign远程调用