Trilogy of Recursive Problem Solving

What is recursion? A program that calls itself repeatedly is recursive.

When I first started to solve the recursive problem, I always struggled with what this layer of function did, what did the next layer of function do after it called itself... Then I felt that implementing a recursive solution was very complicated and fundamental. There is no way to start.

I believe that many beginners are like me, this is a misunderstanding of thinking, we must come out. Since recursion is a process of repeatedly calling itself, it means that the function of each level is the same, so we only need to pay attention to the process of recursive one level.

Insert picture description here
As shown in the figure above, we need to care about the following three points:

  1. The termination condition of the entire recursion.
  2. What does one level of recursion need to do?
  3. What is the return value that should be returned to the upper level?

Therefore, there is also a trilogy for solving recursive problems:

1. Find the termination condition of the entire recursion: when should the recursion end?
2. Find the return value: What information should be returned to the upper level?
3. What should be done in this level of recursion: What should be done in this level of recursion?

Be sure to understand these 3 steps, which are the basis and ideas of the recursive spike algorithm problem in the future.

Specific recommended blog

Guess you like

Origin blog.csdn.net/qq_43229056/article/details/109754662