Recursive call-doubt

I know this is silly but can you please explain how does the recursive call happens in the code below.

void solve(int n,int m)
{

if(m==0)
{

    if(n==x)
        cnt++;

    return;
}

solve(n+1,m-1);
solve(n-1,m-1);

}

let’s say n=10, x=9 and m=4

so this will be the recursion tree

                                10                           m=4
                           /           \
                      9                    11                m=3
                 /        \             /       \
                8           10       10            12        m=2
              /    \       /    \    /    \      /      \
           7        9   11     12 9      11  12          13  m=1     

first solve(n+1,m-1) is called so n will be incremented till m=0;
once m=0 we will do the increment on cnt if the condition is satisfied and return after that and we return to the function and call solve(n-1,m-1) and so we call solve((n-1)+1, (m-1)-1) and so on

1 Like

understood.
thanks mate! :grin:

that’s good to know in case of any doubt feel free to ask me :slight_smile:
if you got the answer mark your answer as resolved !!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.