码迷,mamicode.com
首页 > 其他好文 > 详细

508. Most Frequent Subtree Sum

时间:2018-07-19 13:55:50      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:his   read   current   max   contain   prim   ++   int   ==   

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”.

 

508. Most Frequent Subtree Sum

标签:his   read   current   max   contain   prim   ++   int   ==   

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9335078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!