-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclone-graph.h
47 lines (41 loc) · 972 Bytes
/
clone-graph.h
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
#ifndef CLONE_GRAPH_H_
#define CLONE_GRAPH_H_
#include <unordered_map>
#include <vector>
class Node {
public:
int val;
std::vector<Node*> neighbors;
Node() {
val = 0;
neighbors = std::vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = std::vector<Node*>();
}
Node(int _val, std::vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
// DFS
// Runtime: 4 ms, faster than 93.09% of C++ online submissions for Clone Graph.
// Memory Usage: 8.9 MB, less than 45.65% of C++ online submissions for Clone Graph.
//
class Solution {
public:
Node* cloneGraph(Node* node) {
if (!node) return nullptr;
if (map.count(node) == 0) {
map[node] = new Node(node->val, {});
for (auto neighbor : node->neighbors) {
map[node]->neighbors.push_back(cloneGraph(neighbor));
}
}
return map[node];
}
private:
std::unordered_map<Node*, Node*> map;
};
#endif // !CLONE_GRAPH_H_