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. 谷歌浏览器(chrome)查找、打包已安装的扩展程序

    我们有时候会安装浏览器扩展程序,正常情况下,我们回去谷歌应用商店里面进行下载,但是这个需要VPN,有时候我们没法使用VPN,想从其他已安装扩展程序的浏览器上直接安装就可以使用我们下面这个方法 win1 ...

  2. 【九度OJ】题目1065:输出梯形 解题报告

    [九度OJ]题目1065:输出梯形 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1065 题目描述: 每组测试 ...

  3. 【LeetCode】1006. Clumsy Factorial 解题报告(Python)

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

  4. 【LeetCode】933. Number of Recent Calls 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...

  5. 【LeetCode】437. Path Sum III 解题报告(Python)

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

  6. 【LeetCode】650. 2 Keys Keyboard 只有两个键的键盘(Python)

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

  7. 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  8. P1547逆转,然后再见

    描述 上届的高三在这个暑假终于要到各个城市奔向他们的大学生活了.奇怪的是学校这次异常阔气,说要用三台车子去载他们上学.上届高三的师兄们异常兴奋--可惜的是临行的时候,学校终于露出它"狰狞&q ...

  9. matplotlib 进阶之origin and extent in imshow

    目录 显示的extent Explicit extent and axes limits matplotlib教程学习笔记 import numpy as np import matplotlib.p ...

  10. Golang项目的配置管理——Viper简易入门配置

    Golang项目的配置管理--Viper简易入门配置 What is Viper? From:https://github.com/spf13/viper Viper is a complete co ...