publicint[]findFrequentTreeSum(TreeNode root){if(root ==null)returnnewint[]{};dfs(root);List<Integer> list =newArrayList<>();for(int k : map.keySet()){if(map.get(k)== maxx) list.add(k);}int[] res =newint[list.size()];for(int i =0; i < list.size(); i++) res[i]= list.get(i);return res;}int maxx =0;//出现的最大的次数//记录当前出现的sum 的次数Map<Integer,Integer> map =newHashMap<>();privateintdfs(TreeNode root){if(root ==null)return0;int l =dfs(root.left);int r =dfs(root.right);int s = l + root.val + r;
map.put(s, map.getOrDefault(s,0)+1);
maxx =Math.max(maxx, map.get(s));return s;}