Difference between backtracking and recursion?

前端 未结 7 488
长发绾君心
长发绾君心 2021-01-30 01:47

What is the difference between backtracking and recursion? How is this program working?

void generate_all(int n)
{
    if(n<1) printf("%s\\n", ar);
         


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 02:24

    Recursion is like a bottom-up process. You can solve the problem just by using the result of the sub-problem.

    For example, reverse LinkedList using recursion is just appending a head node on the already reversed sublist.https://leetcode.com/problems/reverse-linked-list/discuss/386764/Java-recursive

    Backtracking is still like a top-down process. Sometimes you can't solve the problem just by using the result of the sub-problem, you need to pass the information you already got to the sub-problems. The answer(s) to this problem will be computed at the lowest level, and then these answer(s) will be passed back to the problem with the information you got along the way.

提交回复
热议问题