当前位置:网站首页>[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);
}
边栏推荐
- SQL必需掌握的100个重要知识点:排序检索数据
- ∫(0→1) ln(1+x) / (x ² + 1) dx
- Go从入门到实战—— 多路选择和超时控制(笔记)
- Go from entry to practice -- CSP concurrency mechanism (note)
- 开源技术交流丨一站式全自动化运维管家ChengYing入门介绍
- C语言程序设计详细版 (学习笔记1) 看完不懂,我也没办法。
- [LeetCode]508. 出现次数最多的子树元素和
- 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
- 100 important knowledge points that SQL must master: using functions to process data
- Burp suite遇到的常见问题
猜你喜欢

∫(0→1) ln(1+x) / (x ² + 1) dx

PCIE知识点-008:PCIE switch的结构

强制 20 天内开发 APP 后集体被裁,技术负责人怒批:祝“早日倒闭!”

100 important knowledge points that SQL must master: sorting and retrieving data

SQL必需掌握的100个重要知识点:使用函数处理数据

List of language weaknesses --cwe, a website worth learning

Go from introduction to actual combat - panic and recover (notes)

猜拳游戏专题训练

Burp suite遇到的常见问题

洛谷P5706 再分肥宅水
随机推荐
我想我要开始写我自己的博客了。
[LeetCode]186. 翻转字符串里的单词 II
数组作业题
PCIE知识点-008:PCIE switch的结构
SQL必需掌握的100个重要知识点:检索数据
[LeetCode]161. 相隔为 1 的编辑距离
Go from introduction to practice -- coordination mechanism (note)
qt base64加解密
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
Go from introduction to actual combat - package (notes)
互联网 35~40 岁的一线研发人员,对于此岗位的核心竞争力是什么?
Process control task
win11桌面出現“了解此圖片”如何删除
石子合并问题分析
Go从入门到实战——Panic和recover(笔记)
神奇的POI读取excel模板文件报错
Go 访问GBase 8a 数据库的一个方法
快递e栈——数组篇小型项目
Go从入门到实战——多态(笔记)