-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClone_Graph.cpp
52 lines (48 loc) · 1.62 KB
/
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
51
52
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
if(node == NULL)
return NULL;
map<int, int> vis;
map<int, UndirectedGraphNode*> newGraphNodes;
UndirectedGraphNode *retNode = NULL;
queue<UndirectedGraphNode *> q;
q.push(node);
vis[node->label] = 1;
while(!q.empty()) {
UndirectedGraphNode *cur = q.front();
q.pop();
UndirectedGraphNode *curNewNode;
if(newGraphNodes.find(cur->label) == newGraphNodes.end()) {
UndirectedGraphNode *newNode = new UndirectedGraphNode(cur->label);
newGraphNodes[cur->label] = newNode;
curNewNode = newNode;
if(!retNode)
retNode = newNode;
} else {
curNewNode = newGraphNodes[cur->label];
}
for(int i = 0; i < cur->neighbors.size(); ++i) {
UndirectedGraphNode *neighbor = cur->neighbors[i];
if(vis.find(neighbor->label) == vis.end()) {
q.push(neighbor);
vis[neighbor->label] = 1;
}
if(newGraphNodes.find(neighbor->label) == newGraphNodes.end()) {
UndirectedGraphNode *newNeighbor = new UndirectedGraphNode(neighbor->label);
newGraphNodes[neighbor->label] = newNeighbor;
}
curNewNode->neighbors.push_back(newGraphNodes[neighbor->label]);
}
}
return retNode;
}
};