-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLC508.cpp
More file actions
162 lines (119 loc) · 3.37 KB
/
Copy pathLC508.cpp
File metadata and controls
162 lines (119 loc) · 3.37 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
508. Most Frequent Subtree Sum
premium lock icon
Companies
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).
*/
/**
* 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 {
private:
std::unordered_map<int, int> freq;
int maxFreq = 0;
int dfs(TreeNode* root) {
if (!root)
return 0;
int left = dfs(root->left);
int right = dfs(root->right);
int sum = left + right + root->val;
maxFreq = max(maxFreq, ++freq[sum]);
return sum;
}
public:
vector<int> findFrequentTreeSum(TreeNode* root) {
if (!root)
return {};
dfs(root);
std::vector<int> ans;
for (auto &p : freq) {
if (p.second == maxFreq)
ans.push_back(p.first);
}
return ans;
}
};
void printVector(vector<int> ans) {
cout << "[ ";
for (int x : ans)
cout << x << " ";
cout << "]\n";
}
int main() {
Solution sol;
//==============================
// Test Case 1
// 5
// / \
// 2 -3
//==============================
TreeNode* root1 = new TreeNode(5);
root1->left = new TreeNode(2);
root1->right = new TreeNode(-3);
cout << "Test Case 1\n";
printVector(sol.findFrequentTreeSum(root1));
//==============================
// Test Case 2
// 5
// / \
// 2 -5
//==============================
sol = Solution();
TreeNode* root2 = new TreeNode(5);
root2->left = new TreeNode(2);
root2->right = new TreeNode(-5);
cout << "Test Case 2\n";
printVector(sol.findFrequentTreeSum(root2));
//==============================
// Test Case 3
// 1
// / \
// 1 1
//==============================
sol = Solution();
TreeNode* root3 = new TreeNode(1);
root3->left = new TreeNode(1);
root3->right = new TreeNode(1);
cout << "Test Case 3\n";
printVector(sol.findFrequentTreeSum(root3));
//==============================
// Test Case 4
//
// 4
// / \
// 2 6
// / \
// 1 3
//==============================
sol = Solution();
TreeNode* root4 = new TreeNode(4);
root4->left = new TreeNode(2);
root4->right = new TreeNode(6);
root4->left->left = new TreeNode(1);
root4->left->right = new TreeNode(3);
cout << "Test Case 4\n";
printVector(sol.findFrequentTreeSum(root4));
//==============================
// Test Case 5
//
// 0
// / \
// 0 0
//==============================
sol = Solution();
TreeNode* root5 = new TreeNode(0);
root5->left = new TreeNode(0);
root5->right = new TreeNode(0);
cout << "Test Case 5\n";
printVector(sol.findFrequentTreeSum(root5));
return 0;
}