Error (vertical order print)

https://ide.codingblocks.com/s/108810 please check for the error.

@samriddhi.srivastava2017
The problem is that you are not returning the map m in any case other than when root==NULL. So any changes made in further calls do not get reflected back in the original map. Also since you are not storing the data from recursive calls and passing it to parent calls , no changes are reflected back and you get error.
There are two solutions to this problem.

  1. You can simply make the return type as void instead of map and pass the map parameter as reference ,
    void vop(node*root,map< int,vector > &m,int d){
    if(root==NULL){
    return ;
    }
    m[d].push_back(root->data);
    vop(root->left,m,d-1);
    vop(root->right,m,d+1);
    }
    Since we are passing through reference , any changes made in the recursive calls will get reflected back in the original map as well.
  2. Alternatively , if you still wish to pass the map without reference and intend to use the return type only , you should take the return from recursive calls as well and return the map m at the end of the function as well.
    map< int , vector< int> > vop(node*root,map< int,vector > m,int d){
    if(root==NULL){
    return m;
    }
    m[d].push_back(root->data);
    m = vop(root->left,m,d-1);
    m = vop(root->right,m,d+1);
    return m ;
    }

Try this and let me know if you face any further problems.

thank you so much sir. Your explanations are really helpful. I have a problem in understanding how the recursive calls work. Could you suggest me how I can I work on this ? I have seen the videos of recursive call as well but still the concept is not very clear.

Sir, can you explain me the difference between- map< int , vector< int> > vop(noderoot,map< int,vector > m,int d){ if(root==NULL){ return m; } m[d].push_back(root->data); m=vop(root->left,m,d-1); m=vop(root->right,m,d+1); return m; } and map< int , vector< int> > vop(noderoot,map< int,vector > m,int d){ if(root==NULL){ return m; } m[d].push_back(root->data); return vop(root->left,m,d-1); return vop(root->right,m,d+1); //return m; }

Sorry, the line did not change. I meant the difference between - https://ide.codingblocks.com/s/109172 and https://ide.codingblocks.com/s/109175

@samriddhi.srivastava2017
Recursion is a tricky topic which might seem difficult at first. I would suggest you to practice all the question in your HackerBlocks contest and Online Portal Challenges first. If that doesn’t make you confident about your Recursion coding skills and want more practice problems , refer to this Recursion specific contest - hack.codingblocks.com/contests/c/714
There’s more questions on this contest to master Recursion in and out with all sorts of problems.
Further I would suggest you to watch this Webinar for explanations and to understand how to approach the Recursion problems .
https://www.youtube.com/watch?v=UaN9WJtbHFY&t=2209s

@samriddhi.srivastava2017
First code snippet :
map< int , vector< int> > vop(node*root,map< int,vector > m,int d){
if(root==NULL){
return m;
}
m[d].push_back(root->data);
return vop(root->left,m,d-1);
return vop(root->right,m,d+1);
//return m;
}

Second code snippet :
map< int , vector< int> > vop(node*root,map< int,vector > m,int d){
if(root==NULL){
return m;
}
m[d].push_back(root->data);
m=vop(root->left,m,d-1);
m=vop(root->right,m,d+1);
return m;
}

The problem with the first snippet is that your code never makes the second recursive call. A function simply returns back to its parent call as soon as it encounters a return statement. Since your code hits a return statement when it calls for the left part in statement
return vop(root->left,m,d-1);
It simply returns from here.
The following recursive call for the right part is simply not made as the control sequence of the program is not shifted back to the parent call and the right part recursive call never executes.
Hence always put your return statements after you have executed all the recursive or other statements in your function as any statement after a return statement will never be executed.

We solve this problem by storing our recursive calls’ answers in our map variable m and then returning it.
m=vop(root->left,m,d-1);
m=vop(root->right,m,d+1);
return m;

This ensures that both the recursive calls are made before we return the control of the program back to the parent call.

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.