Clone Graph
Create a deep copy of every node and edge reachable from a start node.
Why does this pattern fit?
Restate the exact job
Create a deep copy of every node and edge reachable from a start node.
Cycles require remembering a clone before exploring neighbors.
O(V + E) time · O(V) space
Creating the clone after recursion loops forever on cycles.
How to solve Clone Graph
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Create a deep copy of every node and edge reachable from a start node.
Why Graphs: traversal & ordering fits
Cycles require remembering a clone before exploring neighbors.
State to maintain
A map from original node identity to clone.
Transition
Create/store a clone, recursively clone neighbors, and reuse mapped clones.
Time and space
O(V + E) time · O(V) space
Counterexample to the tempting mistake
Creating the clone after recursion loops forever on cycles.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.