Recursion shines for problems with natural recursive structure:
Good for recursion:
- Tree traversal, nested structures
- Divide-and-conquer: merge sort, binary search
- Recursive definitions: factorial, Fibonacci
- Backtracking: maze solving, permutations
Better with iteration:
- Simple counting or accumulation
- Linear data traversal
- When stack depth is a concern
def tree_sum(node):
if node is None:
return 0
return node.value + tree_sum(node.left) + tree_sum(node.right)
When in doubt: if subproblems are the same type as the original, try recursion.