Why the some of the test case are not passing

#include
using namespace std;
class node{
public:
int data ;
nodeleft;
node
right;

node( int d){
    data = d;
    left = NULL;
    right = NULL;
}

};
nodebuilttree(intpre,intin,int s, int e){
static int i = 0;
if(s>e){
return NULL;
}
int idx = -1;
node
root = new node(pre[i]);
for( int j = s; j<= e; j++){
if(pre[i] == in[j]){
idx = j;
break;
}
}
i++;
root->left = builttree(pre,in,s,idx-1);
root->right = builttree(pre,in,idx+1,e);

return root;

}

nodetargetnode(noderoot,int A){
if(root==NULL){
return NULL;
}
nodetemp;
if(root->data == A){
return root;
}
node
ls = targetnode(root->left,A);
if( ls == NULL){
return targetnode(root->right,A);
}
return ls;
}

void printkthlevel(noderoot, int k){
if(root == NULL){
return;
}
if( k==1 ){
cout << root->data << " ";
}
printkthlevel(root->left,k-1);
printkthlevel(root->right,k-1);
return;
}
int printatdistancK(node
root,nodetarget,int k){
if(root == NULL){
return -1;
}
//reac the target node
if(root == target){
printkthlevel(target,k+1);
return 0;
}
// next step - ancestor
int DL = printatdistancK(root->left,target,k);
if(DL!= -1){
//two cases ancestor or
if(DL+1 == k){
cout << root->data << " ";
}
else{
printkthlevel(root->right,k-DL-1);
}
return 1+DL;
}
int DR = printatdistancK(root->right,target,k);
if(DR!= -1){
//two cases ancestor or
if(DR+1 == k){
cout << root->data << " ";
}
else{
printkthlevel(root->left,k-DR-1);
}
return 1+DR;
}
return -1;
}
int main() {
int n;
cin >>n;
int pre[n];
int in[n];
for( int i = 0; i<n; i++){
cin >> pre[i];
}
for( int i = 0; i<n; i++){
cin >>in[i];
}
int t;
cin >> t;
node
root = builttree(pre,in,0,n-1);
while(t–){
int A,k;
cin >> A>>k;
node*target = targetnode(root,A);
int a = printatdistancK(root,target,k);
cout << a << endl;
cout << endl;
}
return 0;
}

hi @akki56756_bab14f919dbad123, code was correct but u need to print the output in sorted order, so store in a vector first and then sort before printing

corrected and commented code https://ideone.com/ODbvDu

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.