LeetCode笔记:515. Find Largest Value in Each Tree Row

原创 2017年03月13日 14:53:00

问题:

You need to find the largest value in each row of a binary tree.

Example:

Input:

Output: [1, 3, 9]

大意:

你需要找到二叉树中每一行最大的值。

例子:

输入:

输出: [1, 3, 9]

思路:

要找每一行最大的数,我们总归是要遍历二叉树的,遍历二叉树分为BFS和DFS,这里我们要找每行最大的数,那么我们就用BFS,一行行分析过去。

还记得我们在传送门:LeetCode笔记:102. Binary Tree Level Order Traversal中,是要求将二叉树一层层地输出出来。那么通过同样的方法,我们用利用队列的先进先出特性,依次保留每一行新读进来的节点。用一个变量记录当前行的总节点数,然后对每一行都去寻找最大的节点值是多少,记录在结果中就可以了。

代码(Java):

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> largestValues(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();

        List<Integer> res = new LinkedList<Integer>();
        if (root == null) return res;

        queue.offer(root);
        while (!queue.isEmpty()) {
            int levelNum = queue.size();
            int temp = queue.peek().val;
            if (queue.peek().left != null) queue.offer(queue.peek().left);
            if (queue.peek().right != null) queue.offer(queue.peek().right);
            queue.poll();
            for (int i = 1; i < levelNum; i++) {
                if (queue.peek().val > temp) temp = queue.peek().val;
                if (queue.peek().left != null) queue.offer(queue.peek().left);
                if (queue.peek().right != null) queue.offer(queue.peek().right);
                queue.poll();
            }
            res.add(temp);
        }
        return res;
    }
}

他山之石:

除了用BFS,其实DFS也可以做,但是就需要有一个参数来记录当前节点所在的行,同时对于每次遇到的新节点,判断该节点值与已经记录的所在行的最大值之间的大小,如果更大就替换掉结果中记录的值,如果小一些那就略过。这个方法运行起来会比我的方法要快。

public class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        helper(root, res, 0);
        return res;
    }
    private void helper(TreeNode root, List<Integer> res, int d){
        if(root == null){
            return;
        }
       //expand list size
        if(d == res.size()){
            res.add(root.val);
        }
        else{
        //or set value
            res.set(d, Math.max(res.get(d), root.val));
        }
        helper(root.left, res, d+1);
        helper(root.right, res, d+1);
    }
}

合集:https://github.com/Cloudox/LeetCode-Record
版权所有:http://blog.csdn.net/cloudox_

版权声明:本文为博主原创文章,允许转载,注明出处,回复告诉博主一声即可。

Find Largest Value in Each Tree Row

题目: You need to find the largest value in each row of a binary tree. Example: Input: ...

515. Find Largest Value in Each Tree Row

题目 You need to find the largest value in each row of a binary tree. Example: Input: ...

【LeetCode】515. Find Largest Value in Each Tree Row【E】【87】

You need to find the largest value in each row of a binary tree. Example: Input: 1...
  • sscssz
  • sscssz
  • 2017年02月19日 18:37
  • 965

leetcode515: Find Largest Value in Each Tree Row

You need to find the largest value in each row of a binary tree. Example: Input: 1...

437. Path Sum III\515. Find Largest Value in Each Tree Row

Path Sum III 题目描述 代码实现 Find Largest Value in Each Tree Row 题目描述 代码实现437. Path Sum III题目描述You are giv...

LeetCode题解(Week5): 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 / ...

Leetcode515. Find Largest Value in Each Tree Row

515. Find Largest Value in Each Tree Row 1、原题 You need to find the largest value in each row of a ...

LeetCode-515. Find Largest Value in Each Tree Row (JAVA)(二叉树每行的最大值)

LeetCode-515. Find Largest Value in Each Tree Row (JAVA)(二叉树每行的最大值)

【LeetCode】515. Find Largest Value in Each Tree Row

问题描述问题链接:https://leetcode.com/problems/find-largest-value-in-each-tree-row/#/descriptionYou need to ...
  • mrbcy
  • mrbcy
  • 2017年03月18日 21:08
  • 579

[DFS]515. Find Largest Value in Each Tree Row

题目: 题目分析: 1、题目要求通过给出的二叉树找出每行最大值。最后通过一个容器返回所有结果。 2、这里注意找的是每一行的最大值,我们知道对于一个数组而言,最大值仅需遍历一遍即可;那么我们把...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode笔记:515. Find Largest Value in Each Tree Row
举报原因:
原因补充:

(最多只允许输入30个字)