Reputation: 73
the following code for printing postorder from given preorder and inorder is showing segmentation fault. can u please explain why??
int j=0;
void post_order(int in[],int pre[],int start,int end){
if(start>end) {
return;
}
int i;
for(int i=start;i<=end;i++){
if(pre[j]==in[i]){
break;
}
}
j++;
post_order(in,pre,start,i-1);
post_order(in,pre,i+1,end);
cout<<in[i]<<" ";
}
void printPostOrder(int in[], int pre[], int n)
{
//Your code here
int start=0;
int end = n-1;
post_order(in,pre,start,end);
}
Upvotes: 0
Views: 203
Reputation: 9682
int i; //< uninitialised
// V--- a new variable called 'i'
for(int i=start;i<=end;i++){
if(pre[j]==in[i]){
break;
}
}
// this will probably crash (i hasn't been initialised yet!)
cout<<in[i]<<" ";
Upvotes: 1