Wrapping Up

As you’ve seen in the filesystem example, recursion is often a great choice for an algorithm in which the algorithm itself doesn’t know on the outset how many levels deep into something it needs to dig.

Now that you understand recursion, you’ve also unlocked a superpower. You’re about to encounter some really efficient—yet advanced—algorithms, and many of them rely on the principles of recursion.

Get A Common-Sense Guide to Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.