Graph Theory37 sections · 1633 units
Open in Course

LeetCode 133 Clone Graph - Why Depth First

Why do we call it that?

DFS is a natural fit for cloning because it fully processes one node before moving on. When you visit a node, you create its copy, then immediately recurse into its neighbors. By the time DFS returns from a neighbor, that neighbor's entire subgraph is already cloned.

This means each recursive call hands back a fully built copy you can attach to the current node's neighbor list. BFS could also work, but DFS gives you a clean recursive structure where the return value is the cloned node itself.