About the difference between recursion and iteration (personal opinion)

First indicate the written explanation of recursion and iteration:

Recursion : Recursion is often used to describe the process of repeating things in a self-similar way. In mathematics and computer science, it refers to the method of using the function itself in the function definition.

Iteration : The activity of repeating the feedback process, with the result of each iteration serving as the initial value for the next iteration.

As far as personal understanding:

1. Recursion means that a big thing is composed of many small, similar things. 4 is composed of two 2s, and 2 is composed of 1. At this time, the big thing is '4', and the smaller thing is '2'. The smallest composition of this thing is '1'. At this time, I want to solve '4' , you need to solve '2' first. Similarly, if you want to solve '2', you need to solve '1' first. Because they belong to the same thing, the solution methods are the same. That is, call yourself until the smallest time, solve it at this time, return to the previous larger thing, solve the problem of the larger thing through the answer of this small thing, and then return. . . And so on.

2. Iteration means, from a small thing, through a method, go round and round, constantly update yourself, keep growing, and finally solve big things. Just like a student's study, first learn the knowledge of the first grade, take the exam, and have the first-grade level; then learn the knowledge of the second grade based on this, take the exam, and have the foundation of the second grade. . . . . . And so on, constantly looping itself, renewing itself.

From my personal point of view, recursion is like a binary tree (also commonly used to solve binary tree problems), recursively from beginning to end.

The iteration is like a spiral, like a ring, but instead of standing still, it is constantly expanding itself.

Guess you like

Origin blog.csdn.net/weixin_45314061/article/details/130031742