-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path133. Clone Graph.cpp
50 lines (49 loc) · 1.49 KB
/
133. Clone Graph.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
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
// DFS
class Solution {
private:
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>m;
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node) return NULL;
if(m.count(node) == 0){
m[node] = new UndirectedGraphNode(node->label);
for(auto x: node->neighbors) m[node]->neighbors.push_back(cloneGraph(x));
}
return m[node];
}
};
// BFS
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(!node) return NULL;
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>m;
UndirectedGraphNode* root = new UndirectedGraphNode(node->label);
m[node] = root;
deque<UndirectedGraphNode*>cur;
deque<UndirectedGraphNode*>next;
cur.push_back(node);
while(!cur.empty()){
UndirectedGraphNode* p = cur.front();
cur.pop_front();
for(auto x: p->neighbors){
if(m.count(x) == 0){
UndirectedGraphNode* copy = new UndirectedGraphNode(x->label);
m[x] = copy;
next.push_back(x);
}
m[p]->neighbors.push_back(m[x]);
}
if(cur.empty()) swap(cur, next);
}
return root;
}
};