-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
29 lines (26 loc) · 708 Bytes
/
Copy pathmain.js
File metadata and controls
29 lines (26 loc) · 708 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
// Definition for undirected graph.
function UndirectedGraphNode(label) {
this.label = label;
this.neighbors = []; // Array of UndirectedGraphNode
}
/**
* @param {UndirectedGraphNode} graph
* @return {UndirectedGraphNode}
*/
var cloneGraph = function(graph) {
if (graph == null) return null;
function helper (graph) {
if (nodes[graph.label] == null) nodes[graph.label] = new UndirectedGraphNode(graph.label)
const cur = nodes[graph.label]
for (const next of graph.neighbors) {
if (nodes[next.label] == null) {
cur.push(helper(next))
} else {
cur.push(nodes[next.label])
}
}
return cur
}
const nodes = {}
return helper(graph)
};