24 / 75
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 structureCommon 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).
💻
Java Solution Hidden
Enable “Show Full Solution” to view the code
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 structureCommon 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