Skip to content
AI360Xpert
Beta
Difficulty: MediumGraphs

Clone Graph

Problem in Plain English

Given one node of a connected undirected graph, build a complete independent copy with the same values and edges. Every original node maps to exactly one new node.

Problem Statement

You are given a reference to a node in a connected undirected graph and must return a deep copy (clone) of the graph.

Each node holds an integer val and a list of its neighbors. Every node is cloned exactly once: revisiting an already cloned node reuses its copy instead of creating another.

Constraints

  • The graph holds between 0 and 100 nodes.
  • 1 <= Node.val <= 100
  • Node.val is unique across the graph.
  • No repeated edges and no self-loops.
  • The graph is connected and all values are valid.

Examples

Example 1

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

Create four new nodes and four new neighbour lists. The cycle returns to the same copy 1C, and nodes 2C and 4C share the same copy 3C; matching values alone would not prove a deep copy.

Example 2

Input
adjList = [[]]
Output
[[]]

The result is a new node with value 1 and a new empty neighbour list. Returning the original isolated node would have the right printed output but violate independence.

Example 3

Input
adjList = []
Output
[]

An empty graph has a null reference, so the clone is also empty.

Example 4

Input
adjList = [[2,3],[1,3],[1,2]]
Output
[[2,3],[1,3],[1,2]]

In this triangle, node 3 is reachable through node 2 and directly from node 1. Both routes reuse the same 3C, and edges back to previously visited nodes reuse their copies rather than recurse again.

Intuition

A deep copy must preserve both connections and sharing: if nodes 2 and 4 both point to node 3 in Example 1, both copied lists must point to the same new node 3C. Keep a map from each original object to exactly one copy. Record an empty copy before visiting its neighbours so the edge back to node 1 can reuse 1C even while its list is unfinished. The traversal then fills new neighbour lists without changing any original object.

Approaches

BFS Clone

Solution Details

Reveal BFS Clone: intuition, complexity, and code

Hints

Hint 1
Two paths can reach the same node. How will you ensure they refer to the same copied object?
Hint 2
Keep a map from original objects to copies, rather than just a visited set. A neighbour reference needs its corresponding copied object.
Hint 3
Create and memoise an empty copy before exploring its neighbours. This lets a back edge reuse the copy immediately; a null input returns null.

Edge Cases

  • A null reference means an empty graph and clones to null.
  • A single node with no neighbours clones to one isolated node.
  • A 100-node chain reaches the maximum recursive depth; a complete 100-node graph instead has 9900 neighbour references that must all be copied.

Common Mistakes and Interview Tips

  • Appending original neighbours to the copy instead of their mapped clones, producing a shallow copy.
  • Creating the copy after recursing into neighbours, which duplicates nodes on every cycle.
  • Skipping the null check and dereferencing the input before the map is even built.
  • Keeping a memo table on the Solution object across public calls returns stale clones and lets two results share objects. Allocate the map inside each call.
  • In JavaScript, repeated queue.shift() moves remaining entries and can lose the linear traversal bound. Use a monotonically increasing head index.
  • Interview tip: show identity and ownership, not only printed values. Two parents of node 3 must share one 3C, while no copied node or neighbour list may belong to the original.

Key Takeaway

Cloning linked structures is a memoised traversal: create the copy on first visit, record it immediately, then wire every neighbour through the map.