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 -3return [2, -3, 4], since all the values happen only once, return all of them in any order.
Examples 2
Input:
5 / \ 2 -5return [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.
我写的代码好长啊,而且用了很多Java里的工具。import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Most_Frequent_Subtree_Sum_508 {
void getSum(TreeNode root){
if(root==null){
return;
}
if(root.left!=null){
getSum(root.left);
root.val+=root.left.val;
}
if(root.right!=null){
getSum(root.right);
root.val+=root.right.val;
}
}
void setNumber(Map<Integer,Integer> map,TreeNode root){
if(root==null){
return;
}
if(map.get(root.val)==null){
map.put(root.val, 1);
}
else{
int num=map.get(root.val);
map.put(root.val, num+1);
}
setNumber(map, root.left);
setNumber(map, root.right);
}
public int[] findFrequentTreeSum(TreeNode root) {
if(root==null){
return new int[]{};
}
getSum(root);
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
setNumber(map, root);
List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
//降序排序
public int compare(Entry<Integer, Integer> o1,
Entry<Integer, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
int max=list.get(0).getValue();
int numOfMax=1;
for(int i=1;i<list.size();i++){
int num=list.get(i).getValue();
if(num==max){
numOfMax++;
}
else{
break;
}
}
int[] result=new int[numOfMax];
for(int i=0;i<numOfMax;i++){
result[i]=list.get(i).getKey();
}
return result;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Most_Frequent_Subtree_Sum_508 m=new Most_Frequent_Subtree_Sum_508();
TreeNode root=new TreeNode(5);
root.left=new TreeNode(2);
root.right=new TreeNode(-5);
int[] result=m.findFrequentTreeSum(root);
for(int i=0;i<result.length;i++){
System.err.print(result[i]+" ");
}
}
}
后来想想很多都是没必要的,比如倒序排序,直接在遍历root的时候用一个int来记录最大出现次数就好了。之后再遍历map,找到value值是那个最大int的即可。还有没必要遍历两次树,在第一次遍历的时候就可以put到map里。
大神就是这样想的,代码简洁好多。
public class Solution {
Map<Integer, Integer> sumToCount;
int maxCount;
public int[] findFrequentTreeSum(TreeNode root) {
maxCount = 0;
sumToCount = new HashMap<Integer, Integer>();
postOrder(root);
List<Integer> res = new ArrayList<>();
for (int key : sumToCount.keySet()) {
if (sumToCount.get(key) == maxCount) {
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 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 = sumToCount.getOrDefault(sum, 0) + 1;
sumToCount.put(sum, count);
maxCount = Math.max(maxCount, count);
return sum;
}
}
Idea is post-order traverse the tree and get sum of every sub-tree, put sum to count mapping to a HashMap. Then generate result based on the HashMap.