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 ...
随机推荐
- 【LeetCode】148. Sort List 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 1340 - Story of Tomisu Ghost
1340 - Story of Tomisu Ghost PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Java 泛型通配符 T,E,K,V,?
Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许开发者在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说所操作的数据类型被 ...
- CS5211替代CH7511B|设计DP转LVDS转接板|替代CH7511B
CH7511B是一款DP转lvds屏转换芯片CH7511B是一款eDP转LVDS转换芯片.CH7511B将嵌入式DisplayPort信号转换为LVDS(低压差分信号).通过CH7511B的高级解码/ ...
- 【SpringBoot】No 'Access-Control-Allow-Origin' header is present on the requested resource.
关键字:跨域,Access-Control-Allow-Origin,转码,解码 在做一个前后端分离项目,本来前端项目都可以正常访问后端接口,跨域是这么设置的,接口可以正常访问 @Configurat ...
- 【17MKH】我在框架中对.Net依赖注入的扩展
说明 依赖注入(DI)是控制反转(IoC)的一种技术实现,它应该算是.Net中最核心,也是最基本的一个功能.但是官方只是实现了基本的功能和扩展方法,而我呢,在自己的框架 https://github. ...
- 《手把手教你》系列技巧篇(五十四)-java+ selenium自动化测试-上传文件-中篇(详细教程)
1.简介 在实际工作中,我们进行web自动化的时候,文件上传是很常见的操作,例如上传用户头像,上传身份证信息等.所以宏哥打算按上传文件的分类对其进行一下讲解和分享. 2.为什么selenium没有提供 ...
- vue 传入后台的数据多了个=
解决方法: 在前端值参时用{} 在后台接收时用Map 来自为知笔记(Wiz)
- 第10组 Alpha冲刺 (2/6)
1.1基本情况 ·队名:今晚不睡觉 ·组长博客:https://www.cnblogs.com/cpandbb/ ·作业博客:https://edu.cnblogs.com/campus/fzu/FZ ...
- jsencrypt vue相关的rsa加密
vue组件引入 import { JSEncrypt } from 'jsencrypt' 方法内使用 let publicKey = asdfsafdadfafasjdhfasfd // 从后台获取 ...