forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwogha95.js
More file actions
74 lines (61 loc) · 1.83 KB
/
wogha95.js
File metadata and controls
74 lines (61 loc) · 1.83 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
68
69
70
71
72
73
74
/**
* TC: O(V + E)
* 모든 정점를 방문하게 되고
* 방문한 정점에서 연결된 간선을 queue에 추가하는 반복문을 실행합니다.
* 따라서 시간복잡도는 정점 + 간선에 선형적으로 비례합니다.
*
* SC: O(V + E)
* memory, visitedNodeVal 에서 V만큼 데이터 공간을 가집니다.
* 그리고 queue에서 최대 모든 간선 갯수 * 2 만큼 갖게 됩니다.
* 따라서 O(V + 2E) = O(V + E)로 계산했습니다.
*
* V: vertex, E: edge
*/
/**
* // Definition for a _Node.
* function _Node(val, neighbors) {
* this.val = val === undefined ? 0 : val;
* this.neighbors = neighbors === undefined ? [] : neighbors;
* };
*/
/**
* @param {_Node} node
* @return {_Node}
*/
var cloneGraph = function (node) {
if (!node) {
return null;
}
const memory = new Map();
const visitedNodeVal = new Set();
return bfsClone(node);
// 1. bfs로 순회하면서 deep clone한 graph의 head를 반환
function bfsClone(start) {
const queue = [start];
const clonedHeadNode = new _Node(start.val);
memory.set(start.val, clonedHeadNode);
while (queue.length > 0) {
const current = queue.shift();
if (visitedNodeVal.has(current.val)) {
continue;
}
const clonedCurrentNode = getCloneNode(current.val);
for (const neighbor of current.neighbors) {
const clonedNeighborNode = getCloneNode(neighbor.val);
clonedCurrentNode.neighbors.push(clonedNeighborNode);
queue.push(neighbor);
}
visitedNodeVal.add(current.val);
}
return clonedHeadNode;
}
// 2. memory에 val에 해당하는 node 반환 (없을 경우 생성)
function getCloneNode(val) {
if (!memory.has(val)) {
const node = new _Node(val);
memory.set(val, node);
return node;
}
return memory.get(val);
}
};