Talentd
FleetCode
Make and follow sheets.Sign in

Depth-First Search Problems (338)

Problems tagged with Depth-First Search. Start with the easy ones, then move to medium and hard.

39 Easy208 Medium91 Hard

Learn these first

1
Tree

Tree traversal problems like preorder, inorder, and postorder are classic DFS applications. These problems build intuition for recursive exploration patterns.

2
Graph

DFS is most commonly applied to graphs. Understanding adjacency lists, edges, and traversal helps you implement DFS for connectivity, cycle detection, and component discovery.

3
Stack

DFS can be implemented iteratively using a stack. Understanding stack behavior helps simulate recursion and optimize traversal in large graphs.

4
Recursion

Most DFS implementations rely on recursion. Learning recursion helps you understand call stacks, base cases, and how DFS naturally explores deep branches.

5
Breadth-First Search

Comparing DFS with BFS helps you choose the right traversal strategy. Many interview problems can be solved with either approach depending on constraints.

TitleSolutionAcceptDifficultyTopics
100. Same TreeVideo
Solution68.0%Easy
101. Symmetric TreeVideo
Solution62.1%Easy
Solution59.2%Easy
112. Path SumVideo
Solution55.8%Easy
113. Path Sum IIVideo
Solution62.6%Medium
Backtracking, Binary Tree +2· 10 companies
Solution46.1%Medium
Array, Breadth-First Search +3· 12 companies
133. Clone GraphVideo
Solution66.1%Medium
156. Binary Tree Upside DownVideoPremium
Solution65.4%Medium
Solution65.2%Medium
Array, Breadth-First Search +3· 86 companies
207. Course ScheduleVideo
Solution52.2%Medium
Solution80.5%Easy
250. Count Univalue SubtreesVideoPremium
Solution57.5%Medium
Solution69.5%Easy
Backtracking, Binary Tree +3· 8 companies
261. Graph Valid TreeVideoPremium
Solution49.9%Medium
269. Alien DictionaryVideoPremium
Solution37.1%Hard
Array, Breadth-First Search +4· 18 companies
285. Inorder Successor in BSTVideoPremium
Solution51.2%Medium
333. Largest BST SubtreeVideoPremium
Solution45.8%Medium
Solution56.3%Medium
Binary Tree, Depth-First Search +2· 13 companies
Solution65.6%Medium
Depth-First Search, Design +4· 19 companies
Showing 1–50 of 338

More about Depth-First Search

Depth-First Search (DFS) is one of the most fundamental traversal techniques used in data structures and algorithms. It explores a graph or tree by going as deep as possible along each branch before backtracking. DFS is commonly implemented using recursion or an explicit stack and is widely used to traverse Graph and Tree structures.

In coding interviews, DFS appears frequently because it naturally models many search and exploration problems. Whether you're checking connectivity in a graph, exploring all paths in a grid, or validating a binary tree property, DFS is often the simplest and most intuitive approach. Top tech companies expect candidates to be comfortable with DFS-based reasoning, especially when combined with Recursion, Backtracking, and other traversal strategies like Breadth-First Search.

As you practice DFS problems, you'll start to recognize common patterns that appear repeatedly in interview questions. Some of the most important DFS techniques include:

  • Recursive traversal for trees and graphs
  • Backtracking exploration for generating combinations or paths
  • Visited set tracking to avoid cycles in graphs
  • Connected component discovery in matrices or adjacency lists
  • Path and state exploration for constraint-based problems

DFS is particularly useful when the solution requires exploring every possible path or deeply analyzing a structure before moving to the next branch. It powers algorithms for cycle detection, topological ordering, strongly connected components, and many grid-based search problems.

On FleetCode, you can master this technique through 333 carefully curated Depth-First Search problems. These exercises range from beginner tree traversals to advanced graph exploration challenges, helping you build strong intuition and interview-ready problem-solving skills.

Depth-First Search: common questions

What are common Depth-First Search patterns?

Common DFS patterns include recursive tree traversal, graph exploration with visited sets, connected component detection, backtracking for path generation, and DFS with memoization for optimization problems.

What is the best way to learn Depth-First Search?

Start by understanding recursive traversal on trees, then practice DFS on graphs and grid-based problems. Focus on recognizing patterns such as visited tracking, recursion depth, and backtracking exploration.

What is the time complexity of Depth-First Search?

For graphs represented with adjacency lists, DFS runs in O(V + E) time where V is the number of vertices and E is the number of edges. The space complexity is typically O(V) due to recursion stack or visited storage.

Is Depth-First Search important for FAANG interviews?

Yes. DFS is a core technique tested frequently in FAANG and other top tech interviews. It appears in graph traversal, tree recursion, backtracking problems, and grid exploration questions.

What are the best Depth-First Search problems for interviews?

The best DFS interview problems typically involve tree traversal, graph connectivity, island counting in grids, path finding, and backtracking. Classic examples include Number of Islands, Path Sum, Course Schedule variants, and graph cycle detection.