Graph Theory37 sections · 1633 units
Open in Course

LeetCode 133 Clone Graph - Strategy

How DFS works

You need to walk through the original graph and build a copy as you go. For each node you visit, create a new node with the same value. Then recursively visit each neighbor and connect the copies.

The tricky part: graphs can have cycles and shared neighbors. If nodes 11 and 33 both connect to node 22, you must reuse the same copied node 22 for both. You cannot create 22 separate copies. Your strategy needs a way to remember which nodes you have already copied.