-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMost Frequent Subtree Sum.cpp
58 lines (42 loc) · 1.43 KB
/
Most Frequent Subtree Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Solution by Rahul Surana
***********************************************************
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).
***********************************************************
*/
#include <bits/stdc++.h>
/**
* 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:
map<int, int> m;
int ma = INT_MIN;
int fs(TreeNode* root){
if(root == NULL) return 0;
int l = fs(root->left);
int r = fs(root->right);
m[l+r+root->val]++;
if(m[l+r+root->val] > ma) ma = m[l+r+root->val];
return l+r+root->val;
}
vector<int> findFrequentTreeSum(TreeNode* root) {
fs(root);
vector<int> ans;
for(auto x: m){
if(x.second == ma) ans.push_back(x.first);
}
return ans;
}
};