What is DFS?
Depth-First Search explores as far as possible along each branch before backtracking. It uses a stack (explicit or via recursion) to track the path and is ideal for problems requiring complete path exploration.Quick Recognition: If you need to explore all paths, find connected components, detect cycles, or perform tree traversals, think DFS!
Pattern Recognition Checklist
Use DFS When
- Need to explore all paths
- Finding connected components
- Detecting cycles in graphs
- Tree traversals (pre/in/post order)
- Backtracking problems
Don't Use When
- Finding shortest path (use BFS)
- Level-order traversal needed
- Path length matters more than existence
- Graph is very deep (stack overflow risk)
When to Use
Tree Traversals
Preorder, inorder, postorder traversals
Path Finding
Finding all paths, checking path existence
Connected Components
Counting islands, detecting cycles
Backtracking
Permutations, combinations, subset generation
Pattern Variations
1. Tree DFS (Recursive)
2. Graph DFS with Visited Set
3. Grid DFS (Island Problems)
4. DFS with Path Tracking
5. Iterative DFS with Stack
Classic Problems
1. Number of Islands
1. Number of Islands
Pattern: Grid DFS marking visited cellsKey: Sink the island as you explore it
2. Clone Graph
2. Clone Graph
Pattern: DFS with HashMap for visited/cloned nodesKey: Create clone first, then connect neighbors
3. Course Schedule
3. Course Schedule
Pattern: Cycle detection with coloring (white/gray/black)Key: Gray nodes indicate cycle in current path
4. Word Search
4. Word Search
Pattern: Grid DFS with backtrackingKey: Mark cell temporarily, restore after exploration
Cycle Detection
Common Mistakes
Debugging Checklist
When your DFS solution fails:1
Check Visited Tracking
Are you marking nodes as visited? Using set or boolean array?
2
Check Base Cases
What stops the recursion? Null node? Visited node?
3
Check Backtracking
Are you restoring state after exploring each path?
4
Check Graph Type
Directed or undirected? Affects cycle detection logic.
5
Check Order
Preorder, inorder, or postorder? Order matters for result.
DFS vs BFS Quick Reference
| Aspect | DFS | BFS |
|---|---|---|
| Data Structure | Stack / Recursion | Queue |
| Path Finding | All paths | Shortest path |
| Space | O(h) height | O(w) width |
| Cycle Detection | Yes (with coloring) | Yes (undirected) |
| Topological Sort | Yes | Yes (Kahn’s) |
| Best For | Deep exploration | Level-by-level |
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Max Depth of Tree | All | Basic tree DFS |
| Same Tree | Amazon | Parallel DFS |
| Path Sum | Meta | Root-to-leaf DFS |
| Invert Binary Tree | Tree modification | |
| Symmetric Tree | Amazon | Mirror DFS |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I’ll use DFS to explore all possibilities.”
- “I’ll maintain a visited set to avoid cycles.”
- “At each node, I’ll [do work] before/after visiting children.”
- “Base case is when [condition] is met.”
- “Time is O(V+E) for graphs, O(n) for trees.”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Find all paths” | DFS with path tracking |
| ”Check if path exists” | Simple DFS |
| ”Find connected components” | DFS on each unvisited |
| ”Detect cycle” | DFS with coloring |
| ”Topological order” | DFS postorder reversal |
Recursive vs Iterative
Recursive vs Iterative
Use Recursive When:
- Tree problems (max depth is log n or limited)
- Code clarity matters
- Natural recursive structure
- Graph can be very deep (avoid stack overflow)
- Need explicit control over stack
- Performance is critical
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Number of Islands | Medium | LeetCode 200 |
| Clone Graph | Medium | LeetCode 133 |
| Course Schedule | Medium | LeetCode 207 |
| Word Search | Medium | LeetCode 79 |
| Surrounded Regions | Medium | LeetCode 130 |
Practice Roadmap
1
Day 1: Tree DFS
- Solve: Max Depth, Path Sum, Same Tree
- Focus: Basic recursive tree traversal
2
Day 2: Grid DFS
- Solve: Number of Islands, Flood Fill
- Focus: 4-directional traversal pattern
3
Day 3: Graph DFS
- Solve: Clone Graph, Course Schedule
- Focus: Visited tracking and cycle detection
4
Day 4: Advanced
- Solve: Word Search, All Paths from Source
- Focus: Backtracking in DFS