← All templates

DFS

Pro

Use when you need to explore a graph or tree recursively by going as deep as possible down each path before exploring other paths.

Keywords for DFS Problems

→ “Explore all paths”
→ “Does a path exist”
→ “Connected components”
→ “Cycle detection”
→ “Traverse entire graph / tree”
→ “Mark visited”

Unlock the DFS template

Get the full code template and worked LeetCode examples for DFS — plus every other pattern in the library — with a one-time $29 lifetime upgrade.

Upgrade to ProSee free Binary Search template →
DFS · Templates · Grind Patterns