当前位置:网站首页>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;
}
边栏推荐
- Tiktok practice ~ sharing module ~ short video download (save to photo album)
- Tiktok practice ~ sharing module ~ copy short video link
- [kubernetes] kubernetes principle analysis and practical application (under update)
- Project practice 5: build elk log collection system
- Tiktok practice ~ search page ~ scan QR code
- 关于不等式取值转义的思路
- 【Kubernetes】Kubernetes 原理剖析与实战应用(更新中)
- Kubernetes 资源拓扑感知调度优化
- Create a time blocker yourself
- Refresh the strong pointer assignment problem in the HP-UX system of Sanguan
猜你喜欢

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

Tiktok practice ~ search page ~ scan QR code

On the origin of the dispute between the tradition and the future of database -- AWS series column

Arduino UNO + DS1302利用31字节静态RAM存储数据并串口打印

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

Project practice 4: user login and token access verification (reids+jwt)

Tiktok practice ~ sharing module ~ copy short video link

Yujun product methodology

Feign远程调用

When does the mobile phone video roll off?
随机推荐
When are global variables initialized before entering the main function?
Tiktok practice ~ sharing module ~ short video download (save to photo album)
Filebeat安装及使用
Summary of several common UML diagrams
知識點總結
[MySQL series] collection of common working SQL (continuous update)
BOM and DOM operations
关于Qt数据库开发的一些冷知识
Redis Basics
JS mobile terminal touch screen event
问题解决:虚拟机无法复制粘贴文件
阿里云个人镜像仓库日常基本使用
一些基本错误
Request method 'POST' not supported
读书笔记:《过程咨询 III》
Microservice architecture
Six necessary threat tracking tools for threat hunters
回溯思路详解
抖音实战~搜索页面~扫描二维码
uni-app使用canvas绘制二维码