-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClones.cpp
191 lines (177 loc) · 4.32 KB
/
Clones.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/
class Solution {
public:
Node* dfs(Node* node,unordered_map<Node*,Node*>& mp)
{
if(node == NULL)
return NULL;
if(mp.find(node) == mp.end())
{
mp[node] = new Node(node->val);
for(Node* neigh: node->neighbors)
{
mp[node]->neighbors.push_back(dfs(neigh,mp));
}
}
return mp[node];
}
Node* cloneGraph(Node* node) {
if(node==NULL)
return node;
unordered_map<Node*,Node*>mp;
return dfs(node,mp);
/*Node* copy = new Node(node->val);
mp[node]=copy;
queue<Node*>q;
q.push(node);
int i;
while(!q.empty())
{
Node* fron = q.front();
q.pop();
for(i=0;i<fron->neighbors.size();i++)
{
Node* neigh = fron->neighbors[i];
if(mp.find(neigh)==mp.end())
{
mp[neigh] = new Node(neigh->val);
q.push(neigh);
}
mp[fron]->neighbors.push_back(mp[neigh]);
}
}
return copy;*/
}
};
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
Node* cloneTree(Node* root) {
if(root==NULL)
return NULL;
Node* copy = new Node(root->val);
for(Node* neigh: root->children)
copy->children.push_back(cloneTree(neigh));
return copy;
}
};
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node() {}
Node(int _val, Node* _next, Node* _random) {
val = _val;
next = _next;
random = _random;
}
};
*/
class Solution {
public:
Node* get(Node* temp)
{
if(temp == NULL)
return NULL;
if(mp.find(temp)!=mp.end())
return mp[temp];
else
{
mp[temp] = new Node(temp->val);
return mp[temp];
}
}
unordered_map<Node*,Node*>mp;
Node* copyRandomList(Node* head) {
if(head == NULL)
return NULL;
if(mp.find(head)!=mp.end())
return mp[head];
mp[head]=new Node(head->val);
mp[head]->next = copyRandomList(head->next);
mp[head]->random = copyRandomList(head->random);
return mp[head];
// Node* temp = head;
// Node* newnode = new Node(temp->val);
// mp[temp]=newnode;
// while(temp!=NULL)
// {
// newnode->next = get(temp->next);
// newnode->random = get(temp->random);
// temp=temp->next;
// newnode=newnode->next;
// }
// return mp[head];
}
};
/**
* Definition for a Node.
* struct Node {
* int val;
* Node *left;
* Node *right;
* Node *random;
* Node() : val(0), left(nullptr), right(nullptr), random(nullptr) {}
* Node(int x) : val(x), left(nullptr), right(nullptr), random(nullptr) {}
* Node(int x, Node *left, Node *right, Node *random) : val(x), left(left), right(right), random(random) {}
* };
*/
class Solution {
public:
NodeCopy* dfs(Node* head,unordered_map<Node*,NodeCopy*>& mp)
{
if(head == NULL)
return NULL;
if(mp.find(head)!=mp.end())
return mp[head];
NodeCopy* copy = new NodeCopy(head->val);
mp[head] = copy;
copy->left = dfs(head->left,mp);
copy->right = dfs(head->right,mp);
copy->random = dfs(head->random,mp);
return copy;
}
NodeCopy* copyRandomBinaryTree(Node* root) {
unordered_map<Node*,NodeCopy*>mp;
if(root == NULL)
return NULL;
return dfs(root,mp);
}
};