How do I get better at recursion and backtracking?

November 14, 2026 5 answers
Asked by 4j~checkout_691

I can follow basic recursive examples, but I get stuck trying to solve subsets on my own. What should I practice to build intuition, and are there any useful resources?

Answers

3x~sidecar_718

Start with what a function call does before tackling branching recursion. Pick a tiny input and write down each call, its arguments, where it pauses, and what it returns. A debugger with a call stack can help too.

For each function, ask three things: what does it promise to return, when can it answer without another call, and how does each call move toward that stopping point? Once you can trace a small case, use that promise to reason about larger cases without trying to hold the whole stack in your head.

s84+porthold_306:

It helps to trace the return trip too. In merge sort, for example, a lot happens after the smaller calls return, when their results are merged.

Reply to this answer
unlisted+on_tuesday

For subsets, draw a decision tree for just two elements. At each element you have two choices: include it or leave it out. When there are no elements left to decide about, record the current subset.

That's the backtracking idea in a nutshell. If you keep one mutable list, add an element before exploring the include branch and remove it afterward, so the next branch starts with the right state. Try listing the four results by hand first, then write the function to match your tree.

4j~checkout_691:

Does the undo step have to happen if each call gets its own list?

Mivrick.74:

No. If each branch gets a separate copy, changing one branch's list won't affect the other. The explicit undo is useful when branches share and modify the same list.

Reply to this answer
Dabblewick.8

Try a progression rather than jumping straight from factorial to permutations. Do a sum or list traversal, then a tree or folder traversal, then subsets, and finally permutations or Tower of Hanoi. Use tiny inputs for each and redraw the calls when you get lost.

After looking at a solution, close it and try again from a blank file. Change the input or ask the function to return a result instead of printing one. Revisit the problem later rather than just recognizing the code in a video.

Reply to this answer
c309~sidewinder_7

For a simple exercise, solve it once with a loop and once recursively. Compare what the loop tracks with the arguments passed to the next call. You can also try subsets with binary choices after you understand the recursive version.

Not every problem needs recursion. A loop or an existing library function may be clearer, while recursively walking nested folders is a natural fit for the structure.

Reply to this answer
Tervick+soon

If videos aren't clicking, switch formats for a bit. Step through code in a debugger or draw the call tree on paper. A language that encourages recursive programming, such as Lisp, is another option, but you don't need to learn a new language to practice.

Later, look at memoization with Fibonacci to see how caching repeated results changes the work. That's a separate idea from backtracking, so there's no need to master it before learning subsets.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions