A Brief Overview of Recursion

When we talk about recursion and computer science, we are talking about a function that calls itself. This type of technique is really good at certain problems because of its ability to maintain state at different levels of recursion.

One thing we want to be careful of with recursion is this idea of stack overflow. This added memory space as we keep calling the same function over and over until we possibly reach a limit and get a stack overflow.

A good rule of thumb is that we should use recursion when it makes code more readable. But sometimes it can be less efficient than looping or iterative solutions so we have to be careful.

There is another good rule that anything you do with a recursion can be done iteratively (loop). And in interviews, there is a good chance you’d be asked the question of the pros and cons of why we might want to use recursive over iterative.

Recursion is interesting and clever but it can be costly, too. So you have to be really careful with it because recursion and space complexity are not friends.

But when it comes to some problems like sorting and tree traversal, you’ll see that using recursive solutions can make things simpler.

Merge sort, quick sort, tree traversal, and graph traversal all use recursion. Most of the time with a tree data structure you’ll want to use recursion. But you’ll also want to be mindful that space complexity or stack overflow can happen.

As always, I highly recommend the Zero to Mastery Academy to improve your own personal skills as a programmer as this is where all this information was drawn from and Andrei Neagoie, the founder and lead instructor, has been a true mentor to me.

Photo credit: https://towardsdatascience.com/recursion-in-python-an-exploration-bfaa452d3260

Excited to learn, and even happier to teach.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Thinking for Voice: Design conversations, not logic

A way to write tech presentations

Ten tips I wish someone gave me when I started out as an engineer

My Mentee Journey at She Code Africa

SheCodeAfrica picture

Looking for #SuperHumans: Humans.ai is expanding the Dev Team

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Operational Excellence

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Excited to learn, and even happier to teach.

More from Medium

CS373 Spring 2022: Blog #8

On a Totemic Ancestor Who Kind of Just Moved in, Early Spring, Whales in a Lake, and Water under…

Scrum Gathering 2015 Notes

CS371P Spring 2022: Dinesh Krishnan Balakrishnan