Can every recursion be changed to iteration?

后端 未结 5 712
遇见更好的自我
遇见更好的自我 2021-01-13 19:14

Is every recursive function convertible to iteration? What characteristic should a recursive function have in order for it to be implemented using iteration?

<
5条回答
  •  时光取名叫无心
    2021-01-13 20:01

    Like the other answers already stated, it's technically possible to do that with simulating the stack. But I guess you don't want to do that. You probably want an iterative solution without using a stack. If that's the case you need to have a tail recursive function. AFAIR that's the only possible way. You can rewrite every tail recursive function to an imperative one without simulating a stack.

提交回复
热议问题