Clone Graph

Medium Solved

Description

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 format / Clarification:

  • Line 1: Graph represented as adjacency list (JSON array)

Example 1:

Input:
[[2,4],[1,3],[2,4],[1,3]]

Output:
[[2,4],[1,3],[2,4],[1,3]]

Example 2:

Input:
[]

Output:
[]

Note:

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.

Test Cases