515 Find Largest Value in Each Tree Row
Problem
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]
Solutions
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new LinkedList<>();
Queue<TreeNode> q = new LinkedList<>();
if (root == null) {
return res;
}
q.add(root);
while (!q.isEmpty()) {
int max = Integer.MIN_VALUE;
int size = q.size();
for (int i = 0; i < size; i ++) {
TreeNode node = q.poll();
max = Math.max(max, node.val);
if (node.left != null) {
q.add(node.left);
}
if (node.right != null) {
q.add(node.right);
}
}
res.add(max);
}
return res;
}
}