Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Concept

Introduction

Go deep, then backtrack · pre/in/post order
step 1 / 20
binary tree
4
2
5
1
3
6
call stack ↓
(returned)
Concept
1dfs(node):
2 if node is null: return
3 visit(node) // PRE-order
4 dfs(node.left) // IN-order sits between the two recursions
5 // (return / backtrack)
6 dfs(node.right) // POST-order acts here, after both
state
  • strategygo deep, then backtrack

line 1Depth-First Search dives down one branch as far as it can go, then BACKTRACKS and tries the next branch. Contrast this with breadth-first, which sweeps level by level. Here we walk the same tree three different ways to reveal the three classic DFS orders.