当前位置:网站首页>515. 在每个树行中找最大值
515. 在每个树行中找最大值
2022-06-26 19:48:00 【北_尘】
接着上个文章,再来一道leetcode的每日一题,题目链接:515. 在每个树行中找最大值
题目如下:
给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。
这道题使用的是二叉树的层序遍历的套路,代码如下:
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();
//这里简单留意一下
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;
}
边栏推荐
- Six necessary threat tracking tools for threat hunters
- Developer survey: rust/postgresql is the most popular, and PHP salary is low
- 股票开户的具体步骤是什么?网上开户安全吗?
- Analysis on development technology of NFT meta universe chain game system
- mysql的充值问题
- 飞天+CIPU体为元宇宙带来更大想象空间
- (树) 树状数组
- 抖音实战~首页视频~下拉刷新
- Project practice 5: build elk log collection system
- 转:苹果CEO库克:伟大的想法来自不断拒绝接受现状
猜你喜欢

Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port

xlua获取ugui的button注册click事件

威胁猎人必备的六个威胁追踪工具

抖音实战~分享模块~生成短视频二维码

Boot指标监测

On the escape of inequality value

数据库范式和主码的选择

项目实战四:用户登录及token访问验证(reids+jwt)

Tiktok practice ~ sharing module ~ generate short video QR code

Tiktok practice ~ search page ~ scan QR code
随机推荐
Feign远程调用
Refresh the strong pointer assignment problem in the HP-UX system of Sanguan
mysql存储过程
Boot的单元测试
Redis single sign on system + voting system
C语言 文件光标 fseek
MySQL recharge
MySQL - database creation and management
知識點總結
Why don't I recommend going to sap training institution for training?
Arduino uno + DS1302 uses 31 byte static RAM to store data and print through serial port
Is it safe to open an account for CICC Wealth Online?
Project practice 5: build elk log collection system
Kubernetes resource topology aware scheduling optimization
Case description: the competition score management system needs to count the competition scores obtained by previous champions and record them in the file. The system has the following requirements: -
xlua获取ugui的button注册click事件
Basic and necessary common plug-ins of vscade
Installation and use of logstash
Some cold knowledge about QT database development
Minimum spanning tree, shortest path, topology sorting, critical path