forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDKhan.cpp
More file actions
31 lines (24 loc) · 851 Bytes
/
PDKhan.cpp
File metadata and controls
31 lines (24 loc) · 851 Bytes
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
class Solution {
public:
void dfs(Node* node, unordered_map<Node*, Node*>& map){
if(map.count(node))
return;
map[node] = new Node(node->val);
for(int i = 0; i < node->neighbors.size(); i++)
dfs(node->neighbors[i], map);
}
Node* cloneGraph(Node* node) {
if(node == NULL)
return NULL;
unordered_map<Node*, Node*> map;
dfs(node, map);
for(auto& x : map){
Node* org = x.first;
Node* dst = x.second;
for(int i = 0; i < org->neighbors.size(); i++){
dst->neighbors.push_back(map[org->neighbors[i]]);
}
}
return map[node];
}
};