Browse curriculum

Clone Graph

Deep copy a connected undirected graph.

Examples

Input:adjList = [[2,4],[1,3],[2,4],[1,3]]
Output:[[2,4],[1,3],[2,4],[1,3]]
Explain:

Return a deep copy of the graph.

Problem Understanding

You are handed one node of a connected, undirected graph.
Build and return a deep copy of the whole graph.

Deep Copy: New nodes, new edges. You cannot return references to the original nodes.

Algorithm Strategy

We need to traverse the entire graph and create a copy of each node exactly once.

Key Tool: HashMap<OldNode, NewNode>.

  1. Check Map: If we already cloned this node, return the NewNode from the map (handles cycles).
  2. Create: If not, create NewNode(val) and add to map.
  3. Recurse: Iterate through OldNode.neighbors. For each neighbor, recursively call clone(neighbor) and add the result to NewNode.neighbors.

Visualization

The original graph is on the left and the copy grows on the right. A copy appears the moment clone creates it and goes into the map straight away. A copy edge is first an arrow — one node’s neighbour list updated — and becomes a plain line when the other end links back. Each call that finds its node already in the map is a map hit: the recursion stops there instead of going round the cycle again.

Dry Run

Input: adjList = [[2,4],[1,3],[2,4],[1,3]] — a square, 1-2-3-4-1.

Step Call Map What happens
1 clone(1) {1} No copy yet: create copy 1 and store it. First neighbour: 2.
2 clone(2) {1, 2} Create and store copy 2. First neighbour: 1.
3 clone(1) {1, 2} 1 is in the map: return copy 1 at once. Copy 2 links to copy 1.
4 clone(3) {1, 2, 3} 2’s next neighbour. Create and store copy 3. Its neighbour 2 is in the map: link 3 → 2.
5 clone(4) {1, 2, 3, 4} Create and store copy 4. Neighbours 1 and 3 are both in the map: link 4 → 1 and 4 → 3. Return copy 4.
6 back in clone(3) — Link 3 → 4. Return copy 3.
7 back in clone(2) — Link 2 → 3. Return copy 2.
8 back in clone(1) — Link 1 → 2. Its next neighbour, 4, is in the map: link 1 → 4. Return copy 1.

Edge Cases & Common Mistakes

Be careful with these scenarios:

  • Empty Graph: Input node is null. Return null immediately.

  • Single Node: A graph with just one node and no neighbors.

  • Cycles: The graph can contain cycles (e.g., A -> B -> A). Infinite recursion happens if you don't use the map to check visited nodes.

  • Reference Identity: You must return a new node instance, not the original reference.

  • Undirected Edge Duplication: In an undirected graph, A-B is one edge, but in adjacency lists, it appears as B in A's neighbors AND A in B's neighbors. Ensure you don't process it infinitely (handled by map).

Solution (DFS)

One map from each original node to its copy. clone checks the map before anything else, and stores a new copy before visiting its neighbours, so a cycle leads back to a copy instead of into endless recursion. The C version uses an array indexed by value as the map: LeetCode guarantees unique values from 1 to 100.

Clone Graph Code
1var cloneGraph = function(node) {
2 if (!node) return null;
3 const copies = new Map();
4
5 function clone(original) {
6 if (copies.has(original)) {
7 return copies.get(original);
8 }
9 const copy = new _Node(original.val);
10 copies.set(original, copy);
11 for (const neighbor of original.neighbors) {
12 copy.neighbors.push(clone(neighbor));
13 }
14 return copy;
15 }
16
17 return clone(node);
18};

The approach, step by step

  1. Check the map

    If the node already has a copy, return that copy.

  2. Create and store

    Create a copy with the same value and put it in the map before visiting neighbours.

  3. Link the neighbours

    For each neighbour, append its clone to the copy’s neighbour list, then return the copy.

Frequently asked questions

Why store the copy in the map before visiting neighbours?

A neighbour can lead straight back to the node being copied. If the copy were stored only after its neighbours were cloned, that cycle would call clone on the same node again and recurse forever.


What makes it a deep copy?

Every node in the result is newly created, and every neighbour list points only at new nodes. Nothing in the copy refers back to the original graph.


Can Clone Graph be solved with BFS?

Yes. Use the same map, create a copy when a node is first discovered, and link neighbours as nodes leave the queue. Both approaches visit every node and edge once, in O(V + E) time.

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description