forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsskey.js
More file actions
34 lines (27 loc) · 686 Bytes
/
hsskey.js
File metadata and controls
34 lines (27 loc) · 686 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
32
33
34
/**
* // 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 oldToNew = new Map();
const dfs = (node) => {
if (oldToNew.has(node)) {
return oldToNew.get(node);
}
const copy = new _Node(node.val);
oldToNew.set(node, copy);
for (let neighbor of node.neighbors) {
copy.neighbors.push(dfs(neighbor));
}
return copy;
};
return dfs(node);
};