当前位置:网站首页>[LeetCode]515. Find the maximum value in each tree row
[LeetCode]515. Find the maximum value in each tree row
2022-06-27 21:50:00 【A Fei algorithm】
subject
515. Find the maximum in each tree row
Given the root node of a binary tree root , Please find the maximum value of each layer in the binary tree .
Example 1:
Input : root = [1,3,2,5,3,null,9]
Output : [1,3,9]
Example 2:
Input : root = [1,2,3]
Output : [1,3]
Tips :
The range of the number of nodes in a binary tree is [0,104]
-231 <= Node.val <= 231 - 1
Method 1: Sequence traversal
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
Queue<TreeNode> q = new LinkedList<>();
if (root != null) q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
int t = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode cur = q.poll();
t = Math.max(t, cur.val);
if (cur.left != null) q.offer(cur.left);
if (cur.right != null) q.offer(cur.right);
}
res.add(t);
}
return res;
}
Method 2:DFS
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
dfs(root, 0, res);
return res;
}
//depth Table the depth of the currently processed node
private void dfs(TreeNode root, int depth, List<Integer> res) {
if (depth == res.size()) {
// The node that first came to this depth
res.add(root.val);
} else {
// Not the first time to this floor
res.set(depth, Math.max(root.val, res.get(depth)));
}
if (root.left != null) dfs(root.left, depth + 1, res);
if (root.right != null) dfs(root.right, depth + 1, res);
}
边栏推荐
- Codeforces Round #719 (Div. 3)
- Go從入門到實戰——接口(筆記)
- GBase 8a的create database 会被查询耗时很长怀疑卡住的现象分析
- Codeforces Round #722 (Div. 2)
- 100 important knowledge points for SQL: in operator
- Icml2022 | scalable depth Gaussian Markov random field
- Go from entry to practice - multiple selection and timeout control (notes)
- 图解基于AQS队列实现的CountDownLatch和CyclicBarrier
- Is it safe to open an account and buy stocks? Who knows
- CEPH distributed storage
猜你喜欢
猜拳游戏专题训练
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
关于异常处理的知识整理
01-Golang-环境搭建
语言弱点列表--CWE,一个值得学习的网站
畅游动态规划之区间DP
[LeetCode]动态规划解拆分整数I[Silver Fox]
100 important knowledge points that SQL must master: filtering data
Go从入门到实战——行为的定义和实现(笔记)
[LeetCode]动态规划解分割数组I[Red Fox]
随机推荐
Flask----应用案例
Special tutorial - Captain selection game
Go 访问GBase 8a 数据库的一个方法
C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
图解基于AQS队列实现的CountDownLatch和CyclicBarrier
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
JVM memory structure when creating objects
[LeetCode]515. 在每个树行中找最大值
GBase 8a数据库用户密码安全相关参数汇总
100 important knowledge points for SQL: in operator
SQL必需掌握的100个重要知识点:用通配符进行过滤
Go from introduction to actual combat - all tasks completed (notes)
Array assignment
qt base64加解密
分享|智慧环保-生态文明信息化解决方案(附PDF)
.NET学习笔记(五)----Lambda、Linq、匿名类(var)、扩展方法
qt 大文件生成md5校验码
Go from entry to practice - multiple selection and timeout control (notes)
gomock mockgen : unknown embedded interface