Skip to content

Resource · Guide or article

Depth-First Search (DFS)

A one-paragraph definition of depth-first search: go as deep as possible along a branch, then backtrack.

Reviewed

Open the original: Depth-First Search (DFS) (opens the original site in a new tab)

Practice this topicOpens the practice interviewer with a question on Trees.

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Reference

What it covers

  • Definition for trees and graphs with an animated visualization
  • Links on tree traversal orders and BFS versus DFS

How to use it

PrepKit study advice, not a claim made by the source.

Write recursive and stack-based DFS for a binary tree, then produce preorder, inorder and postorder output from each.

Scope and limitations

No pseudocode, complexity or examples. Almost all of the teaching is in the three external references.

Access and format

Very short README on GitHub, trekhleb/javascript-algorithms (MIT). English, free.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/tree/depth-first-search/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email