当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

论数据库的传统与未来之争之溯源溯本----AWS系列专栏

刷新三观的HP-UX系统中的强指针赋值出core问题

Review of watermelon book (VII): Bayesian classifier (manual push + code demo)

Solidity - 合约继承子合约包含构造函数时报错 及 一个合约调用另一合约view函数收取gas费用

抖音实战~分享模块~复制短视频链接

Six necessary threat tracking tools for threat hunters

Feitian +cipu body brings more imagination to the metauniverse

Good thing recommendation: mobile terminal development security tool

Development of NFT for digital collection platform

Boot indicator monitoring
随机推荐
Request method 'POST' not supported
微服务版单点登陆系统(SSO)
商品秒杀系统
8VC Venture Cup 2017 - Final Round C. Nikita and stack
Unit test of boot
开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
Good thing recommendation: mobile terminal development security tool
mongoDB的三种基础备份方法
股票开户的具体步骤是什么?网上开户安全吗?
Super VRT
What are the specific steps for opening a stock account? Is it safe to open an account online?
stm32和电机开发(直流有刷电机和步进电机)
Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印
[recommended collection] these 8 common missing value filling skills must be mastered
好物推荐:移动端开发安全工具
抖音实战~分享模块~生成短视频二维码
一些基本错误
【推荐收藏】这8个常用缺失值填充技巧一定要掌握
To: Apple CEO Cook: great ideas come from constantly rejecting the status quo
Microservice architecture