Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Graphs High frequency Solution Available Practice Mode

Clone Graph LeetCode Solution

Tags: #Graph #DFS #BFS #Hash Map

Approach Summary

BFS/DFS with a hash map from original node to its clone. On each visit, create clone if not exists, then clone neighbors.

Practice Mode 2 Progressive Approaches

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Graphs Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Graphs pattern.

Framework: Graphs • Interview Prep: Tested at FAANG • Explore all Graphs problems & invariants →

How to Recognize This Pattern

  • Deep copy of graph
  • Map from original to clone

Complexity Analysis

Time Complexity

O(V + E)

Space Complexity

O(V)

Tags

Graph DFS BFS Hash Map

Frequently Asked Questions

What is the optimal time and space complexity for Clone Graph?
The optimal solution for Clone Graph achieves O(V + E) runtime complexity and requires O(V) auxiliary space. Time Complexity is O(V + E) where V is the number of vertices and E is the number of edges, as every vertex is visited and cloned once and every edge/neighbor link is inspected twice (once per endpoint). Space Complexity is O(V) to store the mapping between original nodes and their clones plus the BFS queue or DFS recursion stack.
How do I identify that Clone Graph requires the Graphs pattern?
In technical interviews, look for key problem signals: Deep copy of graph; Map from original to clone. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Graphs strategy.
What critical edge cases must be handled in Clone Graph?
Essential edge cases to verify include: Null/empty graph — node is null, return null immediately; Single node with no neighbors (val = 1, neighbors = []) — return clone with empty list; Self-loops (node having an edge to itself) — the map ensures the node is not re-cloned indefinitely; Cycles and dense graphs — hash map prevents infinite recursion or re-enqueuing. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Clone Graph?
Clone Graph is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Facebook, Amazon, Google.
Buy me a coffee
Daily Practice
0/0