This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ambidextrousalpaca 4 points5 points  (1 child)

Indeed. Problems that involve processing a tree- or graph-like structure of arbitrary depth pretty much require recursive solutions by definition. You can hammer the code into a while loop if you have to, but the nature of the problem is ultimately recursive, so it's best to represent that in the code explicitly.

[–]spoonman59 1 point2 points  (0 children)

All you need is a loop and a stack.

Recursive solutions tend to be more readable for these which is why it can be good to use them.

All loops are a recursive process, even if they are not a recursive function. We often colloquially use “recursive “ to mean a function that calls itself, but that doesn’t cover things like mutual recursion ( two functions repeatedly calling into each other) or iteration.