package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/find-mode-in-binary-search-tree/
 */
public class Problem501 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

    public int[] findMode(TreeNode root) {
        // TODO
        return new int[0];
    }

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

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(1);
        root.right = new TreeNode(2);
        root.right.left = new TreeNode(2);

        System.out.println(Arrays.toString(prob.findMode(root))); // [1,2]
    }
}

@@ -34,17 +34,116 @@
 il.Array
-s
+List;%0Aimport java.util.Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0A/**%0A 
@@ -410,87 +410,1150 @@
    p
-ublic int%5B%5D findMode(TreeNode root) %7B%0A        // TODO%0A        return new int%5B0%5D
+rivate static class IntRef %7B%0A        int val;%0A    %7D%0A%0A    public int%5B%5D findMode(TreeNode root) %7B%0A        Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        IntRef max = new IntRef();%0A        findMode(root, map, max);%0A        List%3CInteger%3E list = new ArrayList%3C%3E();%0A        map.forEach((val, count) -%3E %7B%0A            if (count == max.val) %7B%0A                list.add(val);%0A            %7D%0A        %7D);%0A        int%5B%5D result = new int%5Blist.size()%5D;%0A        for (int i = 0; i %3C result.length; i++) %7B%0A            result%5Bi%5D = list.get(i);%0A        %7D%0A        return result;%0A    %7D%0A%0A    private void findMode(TreeNode node, Map%3CInteger, Integer%3E map, IntRef max) %7B%0A        if (node == null) %7B%0A            return;%0A        %7D%0A        if (!map.containsKey(node.val)) %7B%0A            if (max.val %3C 1) %7B%0A                max.val = 1;%0A            %7D%0A            map.put(node.val, 1);%0A        %7D else %7B%0A            int newCount = map.get(node.val) + 1;%0A            if (max.val %3C newCount) %7B%0A                max.val = newCount;%0A            %7D%0A            map.put(node.val, newCount);%0A        %7D%0A        findMode(node.left, map, max);%0A        findMode(node.right, map, max)
 ;%0A  

Solve problem 501