what is wrong with my code
Construct tree from preorder
i think you have share wrong code
check and share the correct one
Mistakes
-
you are defining array in wrong way
int n,n1,ino[n],pre[n1];
here n and n1 are not taken from input so can’t use
you use n and n1 first and then take from input later which is wrong -
at line no 24 in your code
for(int j=s; s <= e;j++){
here instead of s it should be i
Modified code
Got it…I have one doubt in createTreeFromTrav function ,in this function why are we giving array inorder and preorder in pointer form as parameters in function.
Also one more doubt how to print the elements in the given output form
it’s simple
you can do it like this
void PrintTree(Node*root) {
if (root == nullptr)
return;
if (root->left)
cout << root->left->data << " => ";
else
cout << "END => ";
cout << root->data;
if (root->right)
cout << " <= " << root->right->data;
else
cout << " <= END";
cout << endl;
PrintTree(root->left);
PrintTree(root->right);
}
Got it…Thank you!!
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.
@dips123deepali_c25f140838182212
kindly give your feeback
you have not given feedback yet
is your doubt is not resolved??
you need further help??