leetcode515. find largest value in each tree row

找出二叉树每层中最大的元素。

515. Find Largest Value in Each Tree Row

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

Example:

1
2
3
4
5
6
7
8
9
Input: 

1
/
3 2
/
5 3 9

Output: [1, 3, 9]

算法:层次遍历的同时记录最大节点值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public List<Integer> (TreeNode root) {
List<Integer> ans = new ArrayList<>();
if (root == null) return ans;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) {
TreeNode p = queue.poll();
if (p.val > max) max = p.val;
if (p.left != null) queue.offer(p.left);
if (p.right != null) queue.offer(p.right);
}
ans.add(max);
}
return ans;
}