Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph.
Each node in the graph contains a value (val)
and a list (neighbors) of its neighbors.
Input: [[2,4],[1,3],[2,4],[1,3]] Output: [[2,4],[1,3],[2,4],[1,3]]
Input: [] Output: []
The output must be a deep copy. Changing the cloned graph should not affect the original.
No submissions yet.
Discuss DFS vs BFS cloning, hash maps for visited nodes, and handling cycles correctly.