Home
DSA Patterns
Depth-First Search
Clone Graph
Clone Graph (LeetCode 133)
Clone Graph (LeetCode 133): deep copy a connected undirected graph.
Examples
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>.
- Check Map: If we already cloned this node, return the
NewNodefrom the map (handles cycles). - Create: If not, create
NewNode(val)and add to map. - Recurse: Iterate through
OldNode.neighbors. For each neighbor, recursively callclone(neighbor)and add the result toNewNode.neighbors.
Visualization
LEFT = ORIGINAL · RIGHT = COPY (1′ COPIES 1) · ARROW = LINKED FROM ONE END SO FAR
CALL clone(1)
Copies
0
Map hits
0
call stack
No clone() running
copies map
Empty
Call clone on node 1, the node LeetCode hands in
Next
clone(1): no copy yet, create a new node with value 1
adjList
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
nodeisnull. Returnnullimmediately.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();45 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 }1617 return clone(node);18};
Clone Graph in Python, Java, C++, TypeScript and C
Clone Graph in Python
class Solution:
def cloneGraph(self, node: Optional['Node']) -> Optional['Node']:
if not node:
return None
copies = {}
def clone(original):
if original in copies:
return copies[original]
copy = Node(original.val)
copies[original] = copy
for neighbor in original.neighbors:
copy.neighbors.append(clone(neighbor))
return copy
return clone(node)Clone Graph in Java
class Solution {
public Node cloneGraph(Node node) {
if (node == null) return null;
Map<Node, Node> copies = new HashMap<>();
return clone(node, copies);
}
private Node clone(Node original, Map<Node, Node> copies) {
if (copies.containsKey(original)) {
return copies.get(original);
}
Node copy = new Node(original.val);
copies.put(original, copy);
for (Node neighbor : original.neighbors) {
copy.neighbors.add(clone(neighbor, copies));
}
return copy;
}
}Clone Graph in C++
class Solution {
public:
Node* cloneGraph(Node* node) {
if (!node) return nullptr;
unordered_map<Node*, Node*> copies;
return clone(node, copies);
}
private:
Node* clone(Node* original, unordered_map<Node*, Node*>& copies) {
if (copies.count(original)) {
return copies[original];
}
Node* copy = new Node(original->val);
copies[original] = copy;
for (Node* neighbor : original->neighbors) {
copy->neighbors.push_back(clone(neighbor, copies));
}
return copy;
}
};Clone Graph in TypeScript
function cloneGraph(node: _Node | null): _Node | null {
if (!node) return null;
const copies = new Map<_Node, _Node>();
function clone(original: _Node): _Node {
if (copies.has(original)) {
return copies.get(original)!;
}
const copy = new _Node(original.val);
copies.set(original, copy);
for (const neighbor of original.neighbors) {
copy.neighbors.push(clone(neighbor));
}
return copy;
}
return clone(node);
}Clone Graph in C
static struct Node* clone(struct Node* original, struct Node** copies) {
if (copies[original->val]) {
return copies[original->val];
}
struct Node* copy = malloc(sizeof(struct Node));
copy->val = original->val;
copy->numNeighbors = original->numNeighbors;
copy->neighbors = malloc((original->numNeighbors + 1) * sizeof(struct Node*));
copies[original->val] = copy;
for (int i = 0; i < original->numNeighbors; i++) {
copy->neighbors[i] = clone(original->neighbors[i], copies);
}
return copy;
}
struct Node* cloneGraph(struct Node* s) {
if (s == NULL) return NULL;
struct Node* copies[101] = {NULL};
return clone(s, copies);
}More practice: DSA interview questions with answers · patterns cheat sheet
