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

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

时间: 2024-10-04 04:57:01

508. Most Frequent Subtree Sum的相关文章

508. Most Frequent Subtree Sum (Medium)

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

508. Most Frequent Subtree Sum 最频繁的子树和

[抄题]: 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 fre

[leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值

遍历二叉树,用map记录sum出现的次数,每一个新的节点都统计一次. 遍历完就统计map中出现最多的sum Map<Integer,Integer> map = new HashMap<>(); public int[] findFrequentTreeSum(TreeNode root) { helper(root); int max = 0; List<Integer> list = new ArrayList<>(); for (int key : m

[LeetCode] Most Frequent Subtree Sum 出现频率最高的子树和

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

[leetcode-508-Most Frequent Subtree Sum]

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

LeetCode Most Frequent Subtree Sum

原题链接在这里:https://leetcode.com/problems/most-frequent-subtree-sum/description/ 题目: 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 subtr

[Swift]LeetCode508. 出现次数最多的子树元素和 | Most Frequent Subtree Sum

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

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.