-
Notifications
You must be signed in to change notification settings - Fork 2
/
133. clone-graph
47 lines (39 loc) · 1.23 KB
/
133. clone-graph
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
// https://leetcode.cn/problems/clone-graph/solutions/370663/ke-long-tu-by-leetcode-solution
// dfs
class Solution {
public:
unordered_map<Node*,Node*> map;
Node* cloneGraph(Node* node) {
if(!node) return NULL;
if(map.find(node)!=map.end()) return map[node];
map[node] = new Node(node->val);
for(int i=0; i<node->neighbors.size(); i++){
map[node]->neighbors.push_back(cloneGraph(node->neighbors[i]) );
}
return map[node];
}
};
// bfs
class Solution {
public:
Node* cloneGraph(Node* node) {
if(!node) return NULL;
unordered_map<Node*,Node*> visited;
queue<Node*>Q;
Q.push(node);
visited[node]=new Node(node->val);
while(!Q.empty()){
Node *n = Q.front();
Q.pop();
for(int i=0; i<n->neighbors.size(); i++){
Node *neighbor = n->neighbors[i];
if(visited.find(neighbor)==visited.end()){
Q.push(neighbor);
visited[neighbor]=new Node(neighbor->val);
}
visited[n]->neighbors.push_back( visited[neighbor] );
}
}
return visited[node];
}
};