leetcode 515. Find Largest Value in Each Tree Row
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> largest_vals = new ArrayList();
helper(root, largest_vals, 0);
return largest_vals;
}
private void helper(TreeNode root, List<Integer> largest_vals, int level){
if (root==null) return;
if (level==largest_vals.size()) {
largest_vals.add(root.val);
} else {
largest_vals.set(level, Math.max(largest_vals.get(level), root.val));
}
helper(root.left, largest_vals, level+1);
helper(root.right, largest_vals, level+1);
}
}