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的更多相关文章

  1. 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 ...

  2. (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 ...

  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 / \ ...

  4. 【LeetCode】515. Find Largest Value in Each Tree Row 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  5. 515 Find Largest Value in Each Tree Row 在每个树行中找最大值

    在二叉树的每一行中找到最大的值.示例:输入:           1         /  \        3   2       /  \    \        5   3    9 输出: [ ...

  6. 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 ...

  7. Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row)

    Leetcode之深度优先搜索(DFS)专题-515. 在每个树行中找最大值(Find Largest Value in Each Tree Row) 深度优先搜索的解题详细介绍,点击 您需要在二叉树 ...

  8. [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 / \ ...

  9. 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 ...

随机推荐

  1. PlatformIO手工升级stcgal到1.6版本

    PlatformIO自带的stcgal版本为1.4, 这个版本只支持到STC15, 不支持STC8. 在使用PlatformIO内建的upload写入STC8A8K64S4A12时, 会提示不识别的协 ...

  2. JAVA使用IDEA本地调试服务的代码

    然后将启动参数的 jdwp=transport=dt_socket,server=y,suspend=n,address=8086 放到服务器上 在执行jar包的命令加入这个 例如 java -jar ...

  3. windows10 下使用 spdlog 总结(spdlog 1.7)

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 注意: 请选择对c++11 支持 完善的编译器, 为什么vs2013不行,因为: spdlo ...

  4. c++设计模式概述之外观

    类写的不够规范,目的是缩短篇幅,请实际中不要这样做. 1.概述 了解外观模式相关概念后,一下子想到的是主板, 主板上有各种元器件,各种指示灯,各种电容,各种电路.然而,主板供电的接口就一个,其他元器件 ...

  5. 【LeetCode】1436. 旅行终点站 Destination City (Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 日期 题目地址:https://leetcod ...

  6. 【LeetCode】1400. 构造 K 个回文字符串 Construct K Palindrome Strings

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计奇数字符出现次数 日期 题目地址:https:// ...

  7. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  8. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. Discrete Logging(poj2417)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5120   Accepted: 2319 ...

  10. 使用.NET 6开发TodoList应用(11)——使用FluentValidation和MediatR实现接口请求验证

    系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 在响应请求处理的过程中,我们经常需要对请求参数的合法性进行校验,如果参数不合法,将不继续进行业务逻辑的处理.我们当然可以将每个 ...