Nodes at distance k from given node


my code is giving wrong output .What mistake i am making

Mohit, you need to make few changes in your code…

  1. Firstly, you are using wrong function while making the tree, since in the question, you are given preorder and inorder array, and you need to build tree using that only…

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 vector to store the result.
  2. Pls write code as per the input and output format asked only…, otherwise your code wont be submitted at all.