-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathClone Graph.cpp
43 lines (42 loc) · 1.47 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
/**
* Definition for undirected graph.
* struct UndirectedGraphNode {
* int label;
* vector<UndirectedGraphNode *> neighbors;
* UndirectedGraphNode(int x) : label(x) {};
* };
*/
class Solution {
public:
/**
* @param node: A undirected graph node
* @return: A undirected graph node
*/
UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
// write your code here
if(node == NULL) return NULL;
unordered_map<UndirectedGraphNode *, UndirectedGraphNode *> nodeMap;
queue<UndirectedGraphNode *> visit;
visit.push(node);
UndirectedGraphNode * nodeCopy = new UndirectedGraphNode(node->label);
nodeMap[node] = nodeCopy;
while (visit.size()>0)
{
UndirectedGraphNode * cur = visit.front();
visit.pop();
for (int i = 0; i< cur->neighbors.size(); ++i)
{
UndirectedGraphNode * neighb = cur->neighbors[i];
if (nodeMap.find(neighb) == nodeMap.end())
{
// no copy of neighbor node yet. create one and associate with the copy of cur
UndirectedGraphNode* neighbCopy = new UndirectedGraphNode(neighb->label);
nodeMap[neighb] = neighbCopy;
visit.push(neighb);
}
nodeMap[cur]->neighbors.push_back(nodeMap[neighb]);
}
}
return nodeCopy;
}
};