当前位置:网站首页>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;
}
}
边栏推荐
- Monotonic stack -42 Connect rainwater
- How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
- Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
- php public private protected
- 【Rust笔记】06-包和模块
- 【Rust 笔记】08-枚举与模式
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- file_ put_ contents
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
猜你喜欢
[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
Dom4j遍历和更新XML
[concurrent programming] consistency hash
Alibaba canal actual combat
状态压缩DP AcWing 291. 蒙德里安的梦想
Log4j2 vulnerability recurrence and analysis
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
Dom4j traverses and updates XML
剑指 Offer II 091. 粉刷房子
[concurrent programming] concurrent tool class of thread
随机推荐
Monotonic stack -84 The largest rectangle in the histogram
Annotations simplify configuration and loading at startup
记忆化搜索 AcWing 901. 滑雪
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
Query XML documents with XPath
Memory search acwing 901 skiing
状态压缩DP AcWing 91. 最短Hamilton路径
【Rust 笔记】11-实用特型
[concurrent programming] concurrent security
Collection interface
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
LeetCode 324. 摆动排序 II
Allocation exception Servlet
Deep parsing JVM memory model
Facial expression recognition based on pytorch convolution -- graduation project
20220630 learning clock in
Complex character + number pyramid
Unity Editor Extension - drag and drop
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made