24 / 75
M

Clone Graph

#24
graphdfsbfs+1

Deep clone undirected graph using DFS/BFS with HashMap to track old→new node mapping.

Example:

Input:adjList = [[2,4],[1,3],[2,4],[1,3]]
Output:Deep copy of the graph with same structure

Common Mistakes:

  • Not handling cycles - must track visited nodes with HashMap
  • Shallow copying instead of deep copying
  • Forgetting to handle null input
  • Creating multiple clones of same node

Notes:

Can use DFS (recursive/iterative) or BFS. Key is HashMap to prevent infinite loops. Time O(V+E), Space O(V).

24/75
Clone Graph