Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.

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

&nbsp;
Example 1:

Input: root = [5,2,-3]
Output: [2,-3,4]


Example 2:

Input: root = [5,2,-5]
Output: [2]


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 104].
	-105 &lt;= Node.val &lt;= 105


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findFrequentTreeSum(self, root: Optional[TreeNode]) -> List[int]:
        def dfs(root):
            
            if not root: return 0
            
            l = dfs(root.left)
            r = dfs(root.right)
            res = root.val + l + r
            
            d[res] += 1
            return res
        
        d = collections.Counter()
        dfs(root)
        maxi = max(d.values())
        return [i for i in d if d[i] == maxi]
        
		# An Upvote will be encouraging
        
        
class Solution {
    public int[] findFrequentTreeSum(TreeNode root) {

        HashMap<Integer,Integer> map=new HashMap<>();
        int sum=sum(root,map);
        int max=0;
        int count=0;
        for(Integer key:map.keySet()){
            max=Math.max(max,map.get(key));
        }

        for(Integer key:map.keySet()){
            if(max==map.get(key)){
                count++;
            }
        }
        int[] ans=new int[count];
        int counter=0;
        for(Integer key:map.keySet()){
            if(max==map.get(key)){
                ans[counter++]=key;
            }
        }

        return ans;

    }
    public int sum(TreeNode root,HashMap<Integer,Integer> map){
        if(root==null)return 0;
        int lh=sum(root.left,map);
        int rh=sum(root.right,map);
        map.put(lh+rh+root.val,map.getOrDefault(lh+rh+root.val,0)+1);
        return lh+rh+root.val;
    }
}
class Solution {
private:
    unordered_map<int, int> m;
    int maxi = 0;
    int f(TreeNode* root){
        if(!root) return 0;
        int l = f(root->left);
        int r = f(root->right);
        int sum = root->val + l + r;
        m[sum]++;
        maxi = max(maxi, m[sum]);
        return sum;
    }
public:
    vector<int> findFrequentTreeSum(TreeNode* root) {
        vector<int> ans;
        f(root);
        for(auto &e : m){
            if(e.second == maxi) ans.push_back(e.first);
        }
        return ans;
    }
};
var findFrequentTreeSum = function(root) {
	const hash = new Map();
	const result = [];
	let max = 0;
	const dfs = (node = root) => {
		if (!node) return 0;
		const { left, right, val } = node;
		const sum = val + dfs(left) + dfs(right);
		const count = hash.get(sum) ?? 0;
		hash.set(sum, count + 1);
		max = Math.max(max, count + 1);
		return sum;
	};

	dfs();
	hash.forEach((value, key) => value === max && result.push(key));
	return result;
};
Most Frequent Subtree Sum