当前位置:网站首页>LeetCode 515. 在每个树行中找最大值
LeetCode 515. 在每个树行中找最大值
2022-07-03 08:49:00 【Sasakihaise_】

【BFS 层次遍历】
class Solution {
// 层次遍历 9:15 9:22
List<Integer> ans = new ArrayList();
public void bfs(TreeNode node){
if(node == null) return;
Queue<TreeNode> queue = new LinkedList();
queue.offer(node);
while(!queue.isEmpty()){
int k = queue.size();
int max = Integer.MIN_VALUE;
while(k-- > 0){
TreeNode top = queue.poll();
max = Math.max(max, top.val);
if(top.left != null) queue.offer(top.left);
if(top.right != null) queue.offer(top.right);
}
ans.add(max);
}
}
public List<Integer> largestValues(TreeNode root) {
bfs(root);
return ans;
}
}【DFS】用一个参数来记录层信息
class Solution {
// DFS 9:38
List<Integer> ans = new ArrayList();
public void dfs(TreeNode node, int t){
if(node == null) return;
if(ans.size() == t) ans.add(node.val);
else ans.set(t, Math.max(ans.get(t), node.val));
dfs(node.left, t + 1);
dfs(node.right, t + 1);
}
public List<Integer> largestValues(TreeNode root) {
dfs(root, 0);
return ans;
}
}
边栏推荐
- On the difference and connection between find and select in TP5 framework
- Character pyramid
- Es8 async and await learning notes
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- Binary tree sorting (C language, char type)
- Concurrent programming (III) detailed explanation of synchronized keyword
- LeetCode 324. 摆动排序 II
- 【Rust 笔记】12-闭包
- PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
- How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
猜你喜欢

Alibaba canal actual combat

Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial

分配异常的servlet

Gif remove blank frame frame number adjustment

樹形DP AcWing 285. 沒有上司的舞會

Really explain the five data structures of redis
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads

LeetCode 75. 颜色分类

Unity Editor Extension - drag and drop

Query XML documents with XPath
随机推荐
Deep parsing (picture and text) JVM garbage collector (II)
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
Binary tree sorting (C language, char type)
Format - C language project sub file
[rust notes] 08 enumeration and mode
[concurrent programming] explicit lock and AQS
C language file reading and writing
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
Summary of methods for counting the number of file lines in shell scripts
Notes and bugs generated during the use of h:i:s and y-m-d
Redux - learning notes
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
ES6 promise learning notes
Unity editor expansion - draw lines
LeetCode 241. 为运算表达式设计优先级
Unity editor expansion - the design idea of imgui
createjs easeljs
[rust notes] 05 error handling
[rust notes] 11 practical features
The difference between if -n and -z in shell