/**
 * 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> maxValues = new LinkedList();
        if(null == root) return maxValues;
        List<TreeNode> nodes = new LinkedList();
        nodes.add(root);
        while(!nodes.isEmpty()) {
            List<TreeNode> tmpNodes = new LinkedList();
            int max = nodes.get(0).val;
            for(TreeNode node : nodes) {
                if(null != node.left) {
                    tmpNodes.add(node.left);
                }
                if(null != node.right) {
                    tmpNodes.add(node.right);
                }
                max = Math.max(max, node.val);
            }
            nodes = tmpNodes;
            maxValues.add(max);
        }
        return maxValues;
    }
}
