Recursion vs loops

I favor recursive solutions when:

  • The implementation of the recursion is much simpler than the iterative solution, usually because it exploits a structural aspect of the problem in a way that the iterative approach cannot

  • I can be reasonably assured that the depth of the recursion will not cause a stack overflow, assuming we’re talking about a language that implements recursion this way

Condition 1 doesn’t seem to be the case here. The iterative solution is about the same level of complexity, so I’d stick with the iterative route.

Leave a Comment