508. Most Frequent Subtree Sum

class Solution {
    int count = 0;
    HashMap<Integer, Integer> map = new HashMap<>(); // map and count all must be here to be accessible from the functions below 
    public int[] findFrequentTreeSum(TreeNode root) {
      
      subSum(root);
      
      List<Integer> res = new ArrayList<>();
      for(int key: map.keySet()){
        if(map.get(key) == count){
          res.add(key);
        }
      }
      
      int[] result = new int[res.size()];
      for(int i = 0; i < res.size(); i++){
        result[i] = res.get(i);
      }
    
      return result;
      
        
    }
    private int subSum(TreeNode root){
      if(root == null){
        return 0;
      }
      
      int left = subSum(root.left);
      int right = subSum(root.right);
      
      int current_sum = left + right + root.val;
      if(!map.containsKey(current_sum)){
        map.put(current_sum, 1);
      }else{
        map.put(current_sum, map.get(current_sum) + 1);
      }
      
      count = Math.max(count, map.get(current_sum));
      
      return current_sum;
    }
      
}




////
 private int postOrder(TreeNode root) {
        if (root == null) return 0;
        
        int left = postOrder(root.left);
        int right = postOrder(root.right);
        
        int sum = left + right + root.val;
        int count = map.getOrDefault(sum, 0) + 1;
        map.put(sum, count);
        
        maxCount = Math.max(maxCount, count);
        
        return sum;
    }
}

//////
getOrDefault
default V getOrDefault(Object key, V defaultValue)

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

NullPointerException - if the specified key is null and this map does not permit null keys

所以 key  可以是 null 吗, 然后 返回 0, 0 + 1 = 1 ????

Parameters:
key - the key whose associated value is to be returned
defaultValue - the default mapping of the key
Returns:
the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key


////
In java “Integer” is a class and “int” is a primitive data type. Now, you might be thinking why Integer class is given if int data type is already available?
I will clear the doubt, in java sometimes you need to convert primitive type of values to object type that time you need class and that’s the reason all primitive data types have their own classes provided in java like,
float → Float(class)
double → Double(class)
char → Character(class) etc.
The concept of converting primitive type to object type is called as “boxing” and vice versa is “unboxing”. These classes are called as “Wrapper classes”.

 

Given the root of a tree, you are asked to find the most frequent subtree sum. 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). 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.

Examples 1
Input:

  5
 /  \
2   -3

return [2, -3, 4], since all the values happen only once, return all of them in any order.

 

Examples 2
Input:

  5
 /  \
2   -5

return [2], since 2 happens twice, however -5 only occur once.

 

Note: You may assume the sum of values in any subtree is in the range of 32-bit signed integer.

posted on 2018-07-19 12:16  猪猪&#128055;  阅读(112)  评论(0)    收藏  举报

导航