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

Fundamentals

The recursion template: base case → recurse → combine
step 1 / 23
binary tree
4
2
5
1
3
call stack ↓
(returned)
Concept
1count(node):
2 push frame
3 if node is null: return 0 // 1. base case
4 L = count(node.left) // 2. recurse
5 R = count(node.right) // 2. recurse
6 return 1 + L + R // 3. combine
state
  • templatebase → recurse → combine

line 1Every tree-DFS is built from the SAME three parts: (1) a base case, (2) recurse into the children, (3) combine their results at the parent. We will count the nodes in this tree to make all three concrete.