-
Notifications
You must be signed in to change notification settings - Fork 0
/
133. Clone Graph.cpp
115 lines (98 loc) · 2.64 KB
/
133. 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// ***
//
// Given a reference of a node in a connected undirected graph.
//
// Return a deep copy (clone) of the graph.
//
// Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
//
// class Node {
// public int val;
// public List<Node> neighbors;
// }
//
//
// Test case format:
//
// For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with val
// == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list.
//
// An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of
// neighbors of a node in the graph.
//
// The given node will always be the first node with val = 1. You must return the copy of the given node as a reference
// to the cloned graph.
//
// ***
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/
// DFS
// See "clone" type questions. All of these questions use the same method.
class Solution {
public:
Node* cloneGraph(Node* node) {
// original node : cloned node
unordered_map<Node*, Node*> m;
return dfs(node, m);
}
private:
Node* dfs(Node* node, unordered_map<Node*, Node*>& m) {
if (not node) {
return nullptr;
}
if (m.count(node)) {
return m[node];
}
Node* clone = new Node(node->val);
m[node] = clone;
for (Node* neighbor : node->neighbors) {
clone->neighbors.push_back(dfs(neighbor, m));
}
return clone;
}
};
// BFS. Making sense of DFS solution is sufficient.
class Solution {
public:
Node* cloneGraph(Node* node) {
if (not node) {
return nullptr;
}
// original node : cloned node
unordered_map<Node*, Node*> m;
queue<Node*> q;
q.push(node);
Node* clone = new Node(node->val);
m[node] = clone;
while (not q.empty()) {
Node* cur = q.front();
q.pop();
for (Node* neighbor : cur->neighbors) {
if (not m.count(neighbor)) {
m[neighbor] = new Node(neighbor->val);
q.push(neighbor);
}
m[cur]->neighbors.push_back(m[neighbor]);
}
}
return clone;
}
};