For almost a year I thought recursion was something only smart people understood. I'd stare
at a function and feel stupid. My trick was tracing every call on paper. Call
1, then call 2, then call 3, and so on. It worked for tiny examples.
Then I hit a tree problem in practice and the paper looked like a spider web.
I gave up that night and went to watch some cricket highlights instead.
What changed it was a friend telling me to stop tracing. He said trust
the call to solve the smaller problem. I thought that was cheating. How can I
trust something I haven't finished writing yet? But think about sum of n. It's
n plus the sum of n minus 1, and the sum of 1 is just
1. You don't need to see all the calls. You only need the base
case and the step to be right. Now I check the base case first, always.
When a recursive function breaks, it's usually that. A missing or wrong base case
gives you endless calls and a stack overflow error. I still trace one or
two levels on paper, but I stop there and trust the rest. It cut
my time on tree questions a lot, I'd say, though I never measured it.
I'm using this for interview practice now, and I feel less scared when a
problem says recursively. Tree traversal was the first one that felt easy after this change.
Backtracking is next on my list, and I know I'll be confused again for a week.
If you're stuck on recursion, tell me which problem broke you and I'll try to explain it.
💻techbhai
Tech Member
Sharing programming, web dev, and hardware tips on Hyd.Circle.