# 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:

Output: [1, 3, 9]

## 代码（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) {

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();
}
}
return res;
}
}

## 他山之石：

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()){
}
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);
}
}

举报原因： 您举报文章：LeetCode笔记：515. Find Largest Value in Each Tree Row 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)