And also I am getting segmentation fault in the code I have written.
How to print the output in the expected form
you just need to follow standard pre order traversal, with a little change.
void preordertr(node *root){
if(!root)
return;
string left = (root->left==NULL)?βENDβ:to_string(root->left->data);
string right = (root->right==NULL)?βENDβ:to_string(root->right->data);
cout<<left<<" => β<data<<β <= "<<right<<endl;
preordertr(root->left);
preordertr(root->right);
}
regarding your code:
there are 2 issues in the code:
-
if(in[i]==in[j]) β¦
this should be if(pre[i]==in[j])β¦ -
input order is a bit different. you read m first, but it should be read later.
corrected code:https://ide.codingblocks.com/s/213440
you can match your code with above updated code to spot your errors.
thanks
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.