Practice Clone Graph
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
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 (int) and a list (List[Node]) of its neighbors. Test case format: For simplicity, each node's value is the same as the node's index (1-indexed). For example, the first node with val == 1, the second node with val == 2, and so on. The graph is represented in the test case using an adjacency list. An adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph. The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.
Source: Clone Graph on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
adjList = [[2,4],[1,3],[2,4],[1,3]]
[[2,4],[1,3],[2,4],[1,3]]
# Example 2
adjList = [[]]
[[]]
# Example 3
adjList = []
[]
Constraints
How to solve Clone Graph
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
Naive recursion revisits nodes forever in a graph with cycles. A map from each original node to its clone serves both as the visited set and as the lookup for wiring clone edges to already-created clones.
Why brute force is not enough
Recursing into neighbors and creating a fresh copy each time never terminates, because every undirected edge is a two-node cycle. Even with a depth bound, you get duplicate copies instead of one shared node per original.
Approach
- If the input node is null, return null; the graph may be empty.
- Clone the start node, record the pair in a map from original to clone, and seed a queue with the start node.
- Pop an original node. For each neighbor, if it has no clone yet, create one, record it in the map, and enqueue the neighbor.
- Append the neighbor's clone, looked up from the map, to the current clone's neighbor list so every edge points at the single shared copy.
- When the queue empties, every reachable node has exactly one clone and all edges are wired. Return the clone of the start node.
Walkthrough of Example 1
adjList = [[2,4],[1,3],[2,4],[1,3]] → [[2,4],[1,3],[2,4],[1,3]]
- Clone node 1, map 1 to its copy, and enqueue 1.
- Process 1: neighbors 2 and 4 are new, so both are cloned and enqueued, and the copy of 1 gets neighbors 2 and 4.
- Process 2: neighbor 1 already has a clone, so just link to it; neighbor 3 is new, so create, enqueue, and link.
- Process 4: neighbors 1 and 3 both have clones, so link both without creating anything.
- Process 3: neighbors 2 and 4 already exist, so link them. The queue is empty; the clone adjacency is [[2,4],[1,3],[2,4],[1,3]].
Time and space
Time O(V + E) · Space O(V). Every node is cloned once and every edge is traversed once from each endpoint; the map and queue hold at most V nodes, where V is the node count and E the edge count.
Interview pitfalls
- Forgetting the null case; an empty graph is a valid input and must return null, not an empty node.
- In the recursive version, insert the clone into the map before recursing into neighbors; inserting afterward lets a cycle re-enter the node and build a duplicate.
- Copying the neighbor list by reference so the clone points at original nodes; every neighbor entry must be a clone.
- Keying the map on node value rather than node identity works here because values are unique, but say that assumption out loud.
Follow-up questions interviewers ask
- How would you clone a directed graph, or one with weighted edges or self-loops?
- If the graph had millions of nodes, how would you avoid recursion depth problems?
- How would you verify in a test that the clone shares no node objects with the original?
Further reading on the technique: Breadth-first search
Related LeetCode Problems
Practice more problems similar to Clone Graph.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.