Recursion
When a function solves a problem by calling itself with a smaller version of the same problem, until it reaches a case it knows how to solve.
Example
The sum from 1 to 4 is 4 + the sum from 1 to 3, which is 3 + the sum from 1 to 2... down to the sum from 1 to 1, which is 1. Result: 10.
Test yourself: Recursion
Iteration and recursion
How does recursion approach a problem?
To add 1 to 100, recursion says "it's 100 plus the sum from 1 to 99". Iteration starts at 1 and keeps adding up to 100.
0/1
â–¶ PlayLessons that use it
Iteration and recursion
2 ways to repeat
Space complexity
How much memory it asks for
Stacks
Last in, first out
Deques
In and out at both ends
Walking a tree
By levels or by branches
Walking a graph
BFS and DFS, now with cycles
Quick sort
A pivot and 2 sides
Merge sort
Split in half and merge in order
Divide and conquer
Split, solve and combine
Rebuilding a tree
Root, left and right
Tower of Hanoi
n − 1, 1 and n − 1
Backtracking
Try, and if it fails, go back
Permutations
Every possible order
N-queens
None attacks another
Dynamic programming
Solve each thing only once
