package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/find-largest-value-in-each-tree-row/
 */
public class Problem515 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public List<Integer> largestValues(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        // TODO
        return result;
    }

    public static void main(String[] args) {
        Problem515 prob = new Problem515();

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(3);
        root.left.left = new TreeNode(5);
        root.left.right = new TreeNode(3);
        root.right = new TreeNode(2);
        root.right.right = new TreeNode(9);
        System.out.println(prob.largestValues(root)); // [1, 3, 9]
    }
}

@@ -57,20 +57,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0A/**%0A 
@@ -498,135 +498,183 @@
     
-// TODO%0A        return result
+Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        largestValues(root, 1, map)
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem515 prob = new Problem515();%0A
+for (int i = 1; i %3C= map.size(); i++) %7B%0A            result.add(map.get(i));%0A        %7D
 %0A   
@@ -682,311 +682,487 @@
     
-T
 re
-eNode root = new TreeNode(1)
+turn result
 ;%0A    
+%7D%0A%0A
     
-root.left = new TreeNode(3);%0A        root.left.left = new TreeNode(5);%0A        root.left.right = new TreeNode(3);%0A        root.right = new TreeNode(2);%0A        root.right.right = new TreeNode(9);%0A        System.out.println(prob.largestValues(root)); // %5B1, 3, 9%5D
+private static void largestValues(TreeNode node, int level,%0A                                      Map%3CInteger, Integer%3E map) %7B%0A        if (node == null) %7B%0A            return;%0A        %7D%0A        if (!map.containsKey(level)) %7B%0A            map.put(level, node.val);%0A        %7D else %7B%0A            map.put(level, Math.max(map.get(level), node.val));%0A        %7D%0A        largestValues(node.left, level + 1, map);%0A        largestValues(node.right, level + 1, map);
 %0A   

Solve problem 515