A Brief Overview of Recursion

Joshycsm
2 min readSep 21, 2020
https://towardsdatascience.com/recursion-in-python-an-exploration-bfaa452d3260

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

--

--