Reqflow
Searching·Intermediate

DFS: Pre-order Traversal

Time O(n)Space O(h)
·

Depth-First Search explores as far down a branch as possible before backtracking. Pre-order DFS visits the current node, then the left subtree, then the right subtree. It uses a stack — either the call stack (recursive) or an explicit one (iterative). Space complexity is O(h) where h is the tree height.

Example: Pre-order DFS traversal of a 7-node binary tree.

When to use this

See also

← All algorithms