/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
Map<Integer,Integer> map=new HashMap<>();
int max=0;
public int[] findFrequentTreeSum(TreeNode root) {
dfs(root);
List<Integer> list= new ArrayList<>();
for(int k : map.keySet()){
if(map.get(k)==max) list.add(k);
}
int n=list.size();
int[] ans= new int[n];
for(int i=0;i<n;i++) ans[i]=list.get(i);
return ans;
}
int dfs(TreeNode root){
if(root==null ) return 0;
int cur = root.val+dfs(root.left)+dfs(root.right);
map.put(cur,map.getOrDefault(cur,0)+1);
max=Math.max(max,map.get(cur));
return cur;
}
}
L508,java,dfs
最新推荐文章于 2024-09-02 20:28:23 发布