-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duplicate_Subtrees.cpp
50 lines (45 loc) · 1.24 KB
/
Duplicate_Subtrees.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
class Solution {
public:
string helper(Node *root, vector<Node*> &ans,
unordered_map<string,int> &mp)
{
string temp = "";
if(root == NULL)
return "#";
temp += to_string(root->data);
temp += helper(root->left,ans,mp);
temp += helper(root->right,ans,mp);
if(mp[temp] == 1)
ans.push_back(root);
mp[temp]++;
return temp;
}
vector<Node*> printAllDups(Node* root) {
// unordered_map<string, int> mp;
// vector<Node*> ans;
// function<string(Node*)> solver = [&](Node* t){
// if(!t){
// return "$";
// }
// string str = "";
// str += to_string(t->data);
// str+="#";
// str+= solver(t->left);
// str+= "#";
// str+= solver(t->right);
// str+= "#";
// mp[str]++;
// if(mp[str]==2){
// ans.push_back(t);
// }
// return str;
// };
// solver(root);
// return ans;
vector<Node*> ans;
unordered_map<string,int> mp;
helper(root,ans,mp);
return ans;
// Code here
}
};