Algorithms

Depth-first search (DFS)

Walking a tree or graph by going down one branch to the bottom before coming back and trying another. Preorder, inorder and postorder are forms of DFS.

Example

With root 1, left 2 (with 4 and 5) and right 3, the preorder is 1, 2, 4, 5, 3.

Test yourself: Depth-first search (DFS)

Walking a tree

In preorder, the root is visited ___ its children.

Lessons that use it

More algorithms terms

All terms →

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.