-
Notifications
You must be signed in to change notification settings - Fork 0
/
potd_1_5.cpp
37 lines (31 loc) · 909 Bytes
/
potd_1_5.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
unordered_map<string, int> mp;
string helper(Node* root) {
if(root->children.size() == 0) {
string str = to_string(root->data);
mp[str]++;
return str;
}
vector<Node*> temp = root->children;
sort(temp.begin(), temp.end(), [](Node* left, Node* right) {
return left->data < right->data;
});
string res = "";
bool first = true;
for(auto child: temp) {
string curr = helper(child);
res += "(" + curr + ")";
}
res += to_string(root->data);
mp[res]++;
return res;
}
int duplicateSubtreeNaryTree(Node *root){
// Code here
if(!root) return 0;
helper(root);
int cnt = 0;
for(auto it: mp) {
if(it.second > 1) cnt++;
}
return cnt;
}