Watch depth-first (in-order, pre-order, post-order) and breadth-first traversals step by step
Choose a traversal type above to see how it works.
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:
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.