Loading problem…
You are implementing a sandboxed recommendation engine and need to copy a user-similarity graph before running mutations.
Each graph node has:
val: unique numeric identifierneighbors: list of adjacent nodesclass Node {
constructor(val, neighbors = []) {
this.val = val;
this.neighbors = neighbors;
}
}Write cloneGraph(node) to return a deep copy of the connected undirected graph rooted at node.
null for empty input// Adjacency list: [[2,3],[1,4],[1,4],[2,3]]
// cloneGraph should return an isomorphic graph with fresh nodes
cloneGraph(node1);