Implement it on LeetCode. The BFS version: text function cloneGraph(node): if node == null: return null visited = {node: new Node(node.val)} queue = [node] while queue not empty: curr = queue.pop() for neighbor in curr.neighbors: if neighbor not in visited: visited[neighbor] = new Node(neighbor.val) queue.add(neighbor) visited[curr].neighbors.add(visited[neighbor]) return visited[node] Both approaches work. The hash map that tracks original → clone is what matters.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/graphs/clone-graph-implementation
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████████████████████████████████████████████████████████████████████████████████