Ideas for writing recursive questions

The first question is, when is the data operated on?

During our recursive process, we must process the data structure, including two time periods, processing data during recursion and processing data during backtracking. The difference between them is that one is written above the call point of the recursive function, and the other is written below the call point. At this time, it should be noted that if the recursive function is returned directly, the data is also processed during recursion.

The second question is, what exactly do we need to return in our recursive function?

It is roughly divided into four major categories:

First, the return data is used during backtracking, and it is used every time a recursive function occurs. At this time, the logic of the recursion and the return value of the final recursion termination condition must be considered.

Second, the data returned when backtracking is the recursive function itself. This situation is quite special. It has an obvious feature, that is, when backtracking to the end, the data returned is still the value returned by the termination condition, so under what circumstances do we Can this case be used? That is, the data we ultimately need to return is the data returned when we recurse to the deepest point;

For example, we reverse the linked list and flip it while recursing. When we reach the end, what we need to return is the last node. At this time, we can use the method mentioned above to return the last node.

Note (and in this case, data is processed in a recursive process;)

Third, the data returned during backtracking is the result of the mutual calculation between the recursive function and the recursive function. This is also the data operation performed during recursion. Then when we return the data, we can calculate it according to the requirements of the question. The trick is that when we write a recursive function here, we can pay attention to the first two recursions, and then you will find that the subsequent recursions will work naturally.

For example, if we judge whether a tree is a symmetric tree, we need the left subtree and the right subtree to be symmetric trees at the same time. Then what we return is the judgment results of the left and right subtrees, and finally return the results of their AND.

Fourth, our recursive function does not need to return data. Its role is only to operate on the data while recursing or to operate on the data during backtracking. For example, in-order traversal of a binary tree, pre-order traversal, post-order traversal, etc.

The third question is to determine the termination condition and what should we return?

It should be noted that the focus of the termination condition is not just the statement to determine whether to terminate. What we return is also very important. Here we need to first consider what data the recursive function returns at the beginning, and then whether during the recursive process You need to use this recursive result and so on, and then prescribe the right medicine.

Summary: Of course, the premise of all this is to first understand the topic well, think about the solution to the problem, and then ask your own ideas on how to answer these three questions. Finally, go through the overall idea again. I believe it is almost the same~

Guess you like

Origin blog.csdn.net/weixin_52394141/article/details/131023366