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

<p>The <strong>subtree sum</strong> 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).</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-3]
<strong>Output:</strong> [2,-3,4]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg" style="width: 207px; height: 183px;" />
<pre>
<strong>Input:</strong> root = [5,2,-5]
<strong>Output:</strong> [2]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>-10<sup>5</sup> &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
</ul>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int subTreeSum(TreeNode *root) {
        if (!root) return 0;
        if (!root->left && !root->right) {
            return root->val;
        }
        return root->val + subTreeSum(root->left) + subTreeSum(root->right);
    }
    vector<int> findFrequentTreeSum(TreeNode* root) {
        vector<int> sums;
        queue<TreeNode*> tree;
        tree.push(root);
        while (!tree.empty()) {
            int treeSize = tree.size();
            for (int i = 0; i < treeSize; i++) {
                TreeNode *cur = tree.front();
                tree.pop();
                int sum = subTreeSum(cur);
                sums.push_back(sum);
                if (cur->left) tree.push(cur->left);
                if (cur->right) tree.push(cur->right);
            }
        }
        unordered_map<int,int> freqMap;
        for (int i = 0; i < sums.size(); i++) {
            freqMap[sums[i]]++;
        }
        vector<pair<int,int>> freqMapVec;
        for (auto it: freqMap) freqMapVec.push_back(it);
        sort(freqMapVec.begin(), freqMapVec.end(), [](pair<int,int> a, pair<int,int> b){
            return b.second < a.second;
        });
        vector<int> ans;
        int maxFreq = freqMapVec[0].second;
        for (auto it : freqMapVec) {
            if (it.second == maxFreq) {
                ans.push_back(it.first);
            }
        }
        /*for (auto it : freqMapVec) {
            cout << it.first << ", " << it.second << endl;
        }*/
        
        return ans;
    }
};
22800