How I Almost Went Bananas with Recursion—And Finally Got It
PRANTA Dutta

PRANTA Dutta @pranta

About: I'm a full-stack developer with 3 years of experience. My focus is Flutter & React Native.

Location:
Chattogram, Bangladesh
Joined:
Dec 17, 2020

How I Almost Went Bananas with Recursion—And Finally Got It

Publish Date: Aug 6
0 0

Chapter 1: Meet the Banana-powered Brain

I was wrestling with this adventurous, tree-like data structure:

var root = new TreeNode(1,
    new TreeNode(2,
        new TreeNode(3),
        new TreeNode(4)
    ),
    new TreeNode(5, null, new TreeNode(8))
);
Enter fullscreen mode Exit fullscreen mode

And I was trying to do an in-order traversal. You remember in-order, right? Left subtree → Node → Right subtree. So for the example tree:

    1
   / \
  2   5
 / \   \
3   4   8
Enter fullscreen mode Exit fullscreen mode

It should visit nodes in this order: [3, 2, 4, 1, 5, 8].


Chapter 2: The Forsaken while (true) Code

I built this monster of a Traverse function:

void Traverse(TreeNode node)
{
    while (true)
    {
        if (node == null) return;
        Traverse(node.left);
        result.Add(node.val);
        node = node.right;
    }
}
Enter fullscreen mode Exit fullscreen mode

It looked kinda right—we go left, then record, then “swing right and loop forever.”
But in reality, this crushed the recursion flow. It never properly returned to earlier calls—it basically hacked the brain messenger system.


Chapter 3: Why It Didn’t Click (AKA Monkey Brain, Cholesterol Edition)

Here’s the meme of my thought process:

  • 🧠 “Okay OK, I know recursion in theory.”
  • 🐒 “My brain’s like bananas + glue when I haven’t used it lately.”
  • 🌀 “Oh man, I forgot call stack is like a stack of sticky notes I’ve gotta peel off!”
  • 😵‍💫 “This code isn’t unwinding properly—why does it never finish??”

So I watched a YouTube video, saw the call stack diagrams, and finally it resonated. I realized the function needed to:

  1. Recurse left
  2. Add current
  3. Recurse right …with each call finishing cleanly—not looping inside one frame.

Chapter 4: The Clean, Proper Solution

Here’s the version that actually slaps:

public static IList<int> InorderTraversal(TreeNode root)
{
    var result = new List<int>();

    void Traverse(TreeNode? node)
    {
        if (node == null) return;
        Traverse(node.left);
        result.Add(node.val);
        Traverse(node.right);
    }

    Traverse(root);
    return result;
}
Enter fullscreen mode Exit fullscreen mode

This exactly mirrors the in-order logic “go left → visit → go right,” and cleverly allows each call frame to fully complete before returning upwards. No weird loops, no internal mutation messing up control flow.


Chapter 5: How to Know When Recursion Isn’t Your Enemy

Recursion only feels scary when it’s invisible. The moment you visualize:

  • “Call Traverse(1)”
  • → push Traverse(1.left)
  • → keep pushing until you hit null
  • → pop stack and record values
  • → pop back and go right...

you realize it’s just a controlled, disciplined flow. Not some magic black box.


Chapter 6: Your Brain Is Fine—You're Learning

A few truths:

  • Everyone nukes recursion logic sometimes. Hell, senior devs draw diagrams.
  • Understanding recursion is about trusting the function will return—with stack frames doing the return work.
  • Spaces between practice make it feel weird—but once you see the pattern again, it snaps into place.

You're simply walking rambunctiously toward mastery, my friend.


TL;DR

  • Problem: while(true) + node = node.right inside recursion broke in-order logic.
  • Solution: Clean recursion with:
  Traverse(left);
  Add current;
  Traverse(right);
Enter fullscreen mode Exit fullscreen mode
  • Realization came when call stack clicked: each function is a sticky-note frame that must fully unwind.

Bonus: Your Codecrafters Plug 🧪

Thanks for pointing out this URL—super helpful if anyone reading this wants hands‑on practice:

Join Codecrafters via this link

Comments 0 total

    Add comment