DEV Community

N Satyadev
N Satyadev

Posted on

Recursion Isn’t Hard. The Call Stack Is Invisible

Ever went through a simple DFS code and went "I would rather beat an old lady with a stick"?

A simple recursive inorder traversal code

That was me until I wasn't.

  • You know what trees are.
  • You also know how the traversals work.
  • Yet you seem to never truly grasp recursive tree traversals.

The problem seems much deeper than "I suck at recursion".

The "Tutorial Hell" Phase

Facing the problem myself, I tried many things — from getting into tutorial hell to solving problems on HackerRank, LeetCode but... does that even work?
In my case, a clear NO.

The Breakthrough: Manual Simulation

The solution is finding out what is actually stopping you from using trees. Play around with it, understand what a call stack is.
Simulate recursion without using recursion (yup, that is a thing) yourself by maintaining a manual call stack on a piece of paper or any programming language.

C function that tracks

A C program that implements inorder traversal while maintaining a manual call stack

Notes that shows maintaining manual call stack on a piece of paper

Now the conclusion is — you understand everything, yet those 3 lines seem magical. That is probably because you are not doing the heavy lifting here.
Recursion takes care of that, and you don't see how.

The Strategy: Walkthrough the problem

The most basic fix? Just be a human.

While developing one of my projects I ran into this exact issue again. What did I do? The exact thing I mentioned above.
I Wrote down the problem, went through it step by step. Made sure I knew what I was looking for before I even touched that damn keyboard.

Let us say the expression is: a=b AND b=c. If someone says, "write code to make it a tree," it might feel like they asked you to poop gold which somehow seems much more reasonable. But it is really not that complex.
The best thing to do at this point is probably ignore all the complex terminologies like LR, LL parsers etc which makes it intimidating. At the core it's all about converting a human thinking process into machine code.

Notes on how a tree can be generated out of expression

The "how" becomes pretty easy from here.
You know AND is the root, and each left and right side is again a sub-tree with its operator as the root.
The simplest approach (to understand recursion) is to manually check the sequence and advance. Each lexeme is just a node with a value and left/right pointers.
A tree is just connections of pointers and recursion is all about returning those pointers in a very strategic way.

Conclusion

Once recursion starts feeling fun — congratulations, it’s over. You’re a nerd now.

If you have been trying to figure out recursion for 67th time now... take a break, touch some grass, come back and make sure you C problems in a human way before attempting to solve like a machine.


PS: Using AI tools like Gemini, Claude, or ChatGPT isn't too bad for understanding such topics either — they add to your existing understanding rather than replacing it.

TLDR: Simulate before you code. Draw before you type. Be a human first, a programmer second.

Top comments (0)