I don't understand error while writing the code for the same


here is my code for Kth distance from target

Lakshay, you need to change few things in your code…

  1. In the question, you are given preorder and inorder arrays and you need to build tree using those arrays only… You can refer to the function as :

node *buildFromInorderandPreorder(int pre[] , int inorder[] , int s , int e)
{
static int i = 0;
if(s > e)
{
return NULL;
}
node *root = new node(pre[i]);
int idx;
for(idx = s ; idx <= e ; idx++)
{
if(pre[i] == inorder[idx])
{
break;
}
}
i++;
root->left = buildFromInorderandPreorder(pre , inorder , s , idx-1);
root->right = buildFromInorderandPreorder(pre , inorder , idx+1 , e);
return root;
}

  1. You need to use the vector to store the result, basically the nodes that you need to print…

  2. Use the same format for input and output as asked in the question…