As we all know, the essence of recursion is similar to the access to stack data. It is advanced, but it is often processed at the end! Furthermore, the local variables of recursive functions are stored in the stack mode. For recursive functions at each layer, the local variables of functions at the current layer are saved in the stack, at the end of the layer recursion function, the data of the original layer can be saved!: For example, if the recursion goes down in sequence, and when the recursion function of this layer is about to enter the next recursive call, the local variables in the function of this layer will be saved, for next use! Well, the above is the data storage method of recursive functions, but sometimes we have to capture the head, recursive words, sometimes difficult to understand, it seems that the total can not be figured out! So I divided each layer of recursive function into three parts. The first part is data processing, judgment, and recursive end judgment before recursive call (of course, the end condition must be before recursive call, otherwise, it will not end after each recursion). The second part is the recursive function itself. The third part: Of course, it is the code for subsequent processing of recursive functions! Here I want to understand one thing. Each layer of function is returned at the end of the previous layer of recursive function, and then processes the rest of the layer of recursive function! Example code: [cpp] # include <iostream> # include <string> using namespace std; int I = 0, j; void reverse (string & s); int main () {string s; cin> s; j = I = s. size (); reverse (s); cout <s <endl; return 0;} void reverse (string & s) {char ch; //.......... the first part .......... I --; ch = s [I]; cout <ch <endl; // here I is a global variable, while ch is a local variable that will be saved in the stack if (-1 = I) return; reverse (s ); // The recursion is regarded as the second part. // The subsequent part is considered as the third part. s [-- j] = ch; // This sentence is returned only when the reverse in the recursive function is returned. In the above Code, each layer will process the following s [-- j] = ch only when reverse () is over; code, because the code above reverse () has been processed every time the recursion goes in, so when the recursion returns, the code below reverse () will naturally be processed, this cycle ends! However, I think the most important thing is that sometimes you don't have to pay attention to the details, but you also need to have a perspective. For example, you only need to know that the function reverse () is to continue to process the same function, there is no need to think about how this function works. I feel crazy! I hope that the tangle of friends will not be entangled in recursion ......... in addition, conditions for Recursive use: There is a termination condition for a recursive call. Each recursive call must be closer and closer to this condition. Only in this way can recursion be terminated. Otherwise, recursion cannot be used! In short, before you use recursion to solve the problem, you must first consider whether the benefits of recursion can compensate him for the cost! Otherwise, iterative algorithms are more efficient than recursive algorithms. Basic Principle of recursion: 1. Every callback function call will return a result. when the program stream is executed at the end of a level-1 recursion, it is transferred to the previous level recursion for further execution. 2. In recursive functions, the statements before recursive calls are in the same order as the called functions at different levels. for example, print statement #1 is located before the recursive call statement. It is executed four times in the order of recursive calls. 3. Each function call has its own local variable. 4. In recursive functions, the execution sequence of statements after a recursive call statement is the opposite of that of the called functions. that is, the statement located before the recursive function entry is executed in the right outer direction. The statement located behind the recursive function entry is executed from the inside out. 5. Although each level of recursion has its own variables, the function code will not be copied. 6. recursive functions must contain statements that can terminate recursive calls. once you understand recursion (understanding recursion, the key is to have a picture of code in your mind. When a function is executed at the entrance of the recursive function, it expands a completely identical piece of code, after executing the expanded code and returning it, continue to execute the code next to the recursive function entry in the previous recursive function). The easiest way to read a recursive function is not to get entangled in its execution process, instead, we believe that recursive functions will successfully complete their tasks. If each step is correct, Your restrictions are set correctly, and each call is closer to the restrictions, recursive functions can always complete the task correctly. The statement itself is not called recursively. The statements executed so far only perform Division operations and test the quotient value. Because these statements are recursively called for repeated execution, their effects are similar to loops: When the quotient value is not zero, its value is used as the initial value to re-start the loop. However, recursive calls save some information (this is different from the loop), so it is better to save the variable value in the stack. This information will soon become very important. The Fibonacci number is a typical recursive case: Fib (0) = 0 [basic condition] Fib (1) = 1 [basic condition] For All integers n> 1: Fib (n) = (Fib (n-1) + Fib (n-2) [recursive definition] recursive algorithms are generally used to solve three types of problems: (1) data is defined by recursion. (Fibonacci function) (2) Problem Solving is implemented by recursive algorithms. (Backtracking) (3) the data structure is defined by recursion. (Tree traversal, Graph Search) for example: procedure a; begin a; end; this method is called directly. another example is procedure B; begin c; end; procedure c; begin B; end; this method is called indirectly. how to Design Recursive Algorithms 1. determine the recursive formula 2. determine boundary (end) Conditions