当前位置:网站首页>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;
}
}
边栏推荐
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- Common DOS commands
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
- 【Rust笔记】05-错误处理
- Gif remove blank frame frame number adjustment
- Development experience and experience
- Convert video to GIF
- Concurrent programming (VI) ABA problems and solutions under CAS
- [RPC] RPC remote procedure call
- Unity editor expansion - window, sub window, menu, right-click menu (context menu)
猜你喜欢

LeetCode 871. 最低加油次数

Binary to decimal, decimal to binary

20220630学习打卡

记忆化搜索 AcWing 901. 滑雪
![[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](/img/76/6561a78b7f883a0e75a53e037153c3.jpg)
[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
![[rust notes] 02 ownership](/img/f7/74f8ea3bd697957f9ebfa3e1513fda.png)
[rust notes] 02 ownership

How to use Jupiter notebook

Really explain the five data structures of redis

Annotations simplify configuration and loading at startup

JS non Boolean operation - learning notes
随机推荐
Development experience and experience
Unity editor expansion - the design idea of imgui
[rust notes] 11 practical features
Alibaba canaladmin deployment and canal cluster Ha Construction
Monotonic stack -503 Next bigger Element II
数据库原理期末复习
使用dlv分析golang进程cpu占用高问题
XPath实现XML文档的查询
Notes and bugs generated during the use of h:i:s and y-m-d
Facial expression recognition based on pytorch convolution -- graduation project
<, < <,>, > > Introduction in shell
LeetCode 241. 为运算表达式设计优先级
[rust notes] 13 iterator (Part 1)
JS non Boolean operation - learning notes
【Rust 笔记】11-实用特型
MySQL index types B-tree and hash
Arbre DP acwing 285. Un bal sans patron.
Divide candy (circular queue)
TP5 multi condition sorting
我们有个共同的名字,XX工