forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobzva.cpp
More file actions
67 lines (58 loc) · 1.62 KB
/
obzva.cpp
File metadata and controls
67 lines (58 loc) · 1.62 KB
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
/**
* 풀이
* - BFS와 해시맵을 사용하여 풀이합니다
*
* Big O
* - N: 주어진 노드의 개수
* - E: 주어진 노드의 간선의 개수
*
* - Time complexity: O(E)
* - 한 Node에서 다른 Node로 향하는 모든 edge를 두번씩 탐색합니다 (두 방향으로 연결되어 있기 때문)
* - Space complexity: O(N)
* - 해시맵에 최대 N개의 노드를 저장합니다
*/
/*
// 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;
}
};
*/
class Solution {
public:
Node* cloneGraph(Node* node) {
if (node == nullptr) return nullptr;
unordered_map<Node*, Node*> node_map;
node_map[node] = new Node(node->val);
queue<Node*> q;
q.push(node);
while (!q.empty()) {
Node* p = q.front();
q.pop();
for (Node* neighbor : p->neighbors) {
// 방문한 적이 없는 노드일 경우
if (node_map.find(neighbor) == node_map.end()) {
// node_map에 새로운 노드를 복제하여 추가
node_map[neighbor] = new Node(neighbor->val);
// 큐에 추가
q.push(neighbor);
}
node_map[p]->neighbors.push_back(node_map[neighbor]);
}
}
return node_map[node];
}
};