515. Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree.
Example:
Input:
1
/ \
3 2
/ \ \
5 3 9
Output: [1, 3, 9]
题目含义:从上到下找到每行的最大值
方法一:DFS
参数d表示层数的索引,第一行对应的d为0
1 private void largestValue(TreeNode root, List<Integer> res, int d){
2 if(root == null){
3 return;
4 }
5 //expand list size
6 if(d == res.size()){
7 res.add(root.val); //在最下面添加新的行
8 }
9 else{
10 //or set value
11 res.set(d, Math.max(res.get(d), root.val)); //当前节点值处于第(d+1)层,如果当前值大于res中保存的值,则替换成最大值
12 }
13 largestValue(root.left, res, d+1);
14 largestValue(root.right, res, d+1);
15 }
16
17
18 public List<Integer> largestValues(TreeNode root) {
19 List<Integer> res = new ArrayList<Integer>();
20 largestValue(root, res, 0);
21 return res;
22 }
方法二:
1 public List<Integer> largestValues(TreeNode root) {
2 Queue<TreeNode> queue = new LinkedList<TreeNode>();
3 List<Integer> res = new ArrayList<Integer>();
4 if (root == null) return res;
5 queue.add(root);
6 while (!queue.isEmpty()) {
7 int largestElement = Integer.MIN_VALUE;
8 int queueSize = queue.size();
9 for (int i=0;i<queueSize;i++) {
10 TreeNode cur = queue.poll();
11 largestElement = Math.max(cur.val, largestElement);
12 if (cur.left != null) queue.add(cur.left);
13 if (cur.right != null) queue.add(cur.right);
14 }
15 res.add(largestElement);
16 }
17 return res;
18 }
515. Find Largest Value in Each Tree Row的更多相关文章
- LN : leetcode 515 Find Largest Value in Each Tree Row
lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...
- (BFS 二叉树) leetcode 515. Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...
- 515. Find Largest Value in Each Tree Row查找一行中的最大值
[抄题]: You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ ...
- 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 515 Find Largest Value in Each Tree Row 在每个树行中找最大值
在二叉树的每一行中找到最大的值.示例:输入: 1 / \ 3 2 / \ \ 5 3 9 输出: [ ...
- LeetCode 515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
515. 在每个树行中找最大值 515. Find Largest Value in Each Tree Row 题目描述 You need to find the largest value in ...
- Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)
Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...
- [leetcode-515-Find Largest Value in Each Tree Row]
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ ...
- leetcode算法: Find Largest Value in Each Tree Row
'''You need to find the largest value in each row of a binary tree.Example:Input: 1 / \ 3 2 / \ \ 5 ...
随机推荐
- JAVA将Object数组转换为String数组
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; java将Object[ ...
- JAVA结合 JSON Web Token(JWT) 工具类
引入java-jwt-3.3.0.jar . jjwt-0.9.0.jar .jackson-all-1.7.6.jar 或者maven <!-- https://mvnrepository. ...
- libevent之学习文档
1.官方文档 2.中文版 3.gitbook
- 1017 - Brush (III)
1017 - Brush (III) PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Sami ...
- leetcode日记本
写在前面: 2019.6开始经过一年的学习,我依然没有学会算法,依然停留在最基本的阶段,面对题目依然一头雾水 但是难不是放弃的理由,根据毛主席的论持久战原理,我决定一天看一点循序渐进,相信总有一天可以 ...
- 中华古诗词知识图谱之实体关系构建&导入neo4j数据库
实体分析 诗名实体 属性 包含:作诗时间,诗名,内容,翻译,背景. 关系 实体1 关系 实体2 诗名 形式 诗词形式 诗名 作者 诗人 诗名 分类 类别 诗名 词牌名 词牌名 诗名 曲牌名 曲牌名 诗 ...
- Ranger-AdminServer安装Version2.0.0
Ranger-AdminServer安装, 对应的Ranger版本2.0.0. 1.安装规划 RangerAdmin安装依赖如下组件: mysql solr IP/机器名 安装软件 运行进程 dap2 ...
- Docker基本命令入门
本文介绍Docker基本的操作命令,包括对镜像容器的获取,查看,保存,启停等操作. 1. 检查docker的版本: docker version docker -v 2. 搜索名字叫做tutorial ...
- 计算机系统3-> 现代计算机基石 | 图灵机理论
在理解CPU之前,我们有必要先了解一下现代计算机理论的基石--图灵机,这个抽象模型决定了现代计算机可以被实现.这个模型的工作原理也投射到了CPU的工作实现上.图灵机的知识可深可浅,换句话说,上手容易, ...
- elasticsearch之警惕inner hits的性能问题
一.inner hits简介 elasticsearch提供了nested数据类型来处理主子文档的问题,可以解决子文档字段被分裂平铺导致字段之间失去了整体的关联性: elasticsearch提供的i ...