Tree Traversal Visualization

Watch depth-first (in-order, pre-order, post-order) and breadth-first traversals step by step

Speed: 800ms
Select a traversal to begin

Traversal Output

Start a traversal to see the visited order

Algorithm

Choose a traversal type above to see how it works.

Depth-First Search (DFS) vs Breadth-First Search (BFS)

Depth-First Search (DFS)

Goes as deep as possible down each branch before backtracking to explore the next branch. Uses a stack (or recursion, which is an implicit stack).

All three recursive traversals are variations of DFS — they only differ in when the node is visited relative to its children:

  • Pre-order: Visit node, then children
  • In-order: Visit left child, then node, then right child
  • Post-order: Visit children, then node

Breadth-First Search (BFS)

Visits all nodes at the current depth level before moving to the next level (left to right, top to bottom). Uses a queue — no backtracking.

Also called level-order traversal. Each node is dequeued, processed, then its children are enqueued for the next level.

Unlike DFS, BFS never backtracks — it simply moves forward through the queue, one level at a time.