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   9 

Output: [1, 3, 9]

讲解

又是树的层次遍历题。相同的题我已经做了两个了:637. Average of Levels in Binary Tree、429. N-ary 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; }
 * }
 */
class Solution {
    List result = new ArrayList<>();
    public List largestValues(TreeNode root) {
        if(root==null){
            return result;
        }
        Queue queue = new LinkedList<>();
        queue.offer(root);
        int count = 1;
        while(!queue.isEmpty()){
            List list = new ArrayList<>();
            int floorSize = 0;
            for(int i=0;i

你可能感兴趣的:(树形结构,广度优先搜索,算法,leetcode)