@@ -0,0 +1,1485 @@
+/**%0A * Given the root of a tree, you are asked to find the most frequent subtree sum.%0A * The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).%0A * So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.%0A * %0A * Solution: Use DFS + recursion and hashmap to maintain frequency%0A * Complexity: O(N) time and space.%0A */%0Apublic class Solution %7B%0A  public int%5B%5D findFrequentTreeSum(TreeNode root) %7B%0A    if (root == null) return new int%5B0%5D;%0A    HashMap%3CInteger, Integer%3E map = new HashMap%3CInteger, Integer%3E();%0A    populateMap(root, map);%0A    int max = 0;%0A    for (int i : map.keySet()) %7B%0A      max = Math.max(max, map.get(i));%0A    %7D%0A%0A    List%3CInteger%3E list = new ArrayList%3CInteger%3E();%0A    for (int i : map.keySet()) %7B%0A      if (map.get(i) == max) list.add(i);%0A    %7D%0A%0A    int%5B%5D res = new int%5Blist.size()%5D;%0A    for (int i = 0; i %3C list.size(); i ++) res%5Bi%5D = list.get(i);%0A    return res;%0A  %7D%0A%0A  public int populateMap(TreeNode node, HashMap%3CInteger, Integer%3E map) %7B%0A    if (node == null) return 0;%0A    if (node.left == null && node.right == null) %7B%0A      map.put(node.val, map.getOrDefault(node.val, 0) + 1);%0A      return node.val;%0A    %7D%0A    int left = populateMap(node.left, map);%0A    int right = populateMap(node.right, map);%0A    int sum = left + right + node.val;%0A    map.put(sum, map.getOrDefault(sum, 0) + 1);%0A    return sum;%0A  %7D%0A%7D

Add frequent subtree sum