-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclone-graph.cc
41 lines (37 loc) · 1.26 KB
/
clone-graph.cc
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
/**
* 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 == nullptr) return nullptr;
unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> node_map;
queue<UndirectedGraphNode*> q;
UndirectedGraphNode *cur(nullptr), *new_node(nullptr);
new_node = new UndirectedGraphNode(node->label);
node_map[node] = new_node;
q.push(node);
while(!q.empty()) {
cur = q.front(); q.pop();
for(auto x: cur->neighbors) {
if(node_map.find(x) != node_map.end()) {
//visited
node_map[cur]->neighbors.push_back(node_map[x]);
}
else {
//not visited
new_node =new UndirectedGraphNode(x->label);
node_map[x] = new_node;
node_map[cur]->neighbors.push_back(node_map[x]);
q.push(x);
}
}
}
return node_map[node];
}
};