LC 133
LeetCode 133 · Medium

Clone Graph

Return a deep copy of a connected undirected graph.

DFS → · High frequency · Solve on LeetCode →

Try it

Step through the core mechanic. The simulator below runs the dfs shape this problem is built on.

Walk the pattern

No dedicated step-through for this one yet. The shape is DFS — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.

The approach

DFS/BFS with a hash map from original node to its clone. Create the clone on first visit; the map both prevents infinite loops on cycles and lets you wire neighbour links.

AspectValue
PatternDFS
Recognise it byDeep-copy a graph with cycles.
Time complexityO(V+E)
Space complexityO(V)
DifficultyMedium

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.