not passing all the test cases
Nodes at distance k
@pradyumn25jain
Can you elaborate on the idea behind of this step?
// check presence of target node in both left and right sub tree
int left = kth_distance(root->left,k,target);
if(left != -1){
// case 2 - ancesstor
if((k-left)==1){
cout<<root->data<<" ";
}else{
// case 3 - opp sub tree
node_at_level(root->right,(k-2-left));
}
return left+1;
}
int right = kth_distance(root->right,k,target);
if(right != -1){
// case 2 - ancesstor
if((right+1)==k){
cout<<root->data<<" ";
}else{
// case 3 - opp sub tree
node_at_level(root->left,(k-2-right));
}
return right+1;
}
this is because the target node can be present in either left or right sub tree