#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
data = d;
left = NULL;
right = NULL;
}
};
node *createTreeFromTrav(int *in, int *pre, int s, int e)
{
static int i = 0;
//Base Case
if (s > e)
{
return NULL;
}
//Rec Case
node *root = new node(pre[i]);
int index = -1;
for (int j = s; j <= e; j++)
{
if (in[j] == pre[i])
{
index = j;
break;
}
}
i++;
root->left = createTreeFromTrav(in, pre, s, index - 1);
root->right = createTreeFromTrav(in, pre, index + 1, e);
return root;
}
void PrintAtLevelK(node* root,int k)
{
if(root==NULL)
{
return;
}
if(k==0)
{
cout<<root->data<<" ";
}
PrintAtLevelK(root->left,k-1);
PrintAtLevelK(root->right,k-1);
return;
}
int PrintAtDistanceK(node* root,node* target,int k)
{
//base case
if(root==NULL)
{
return -1;
}
// reach the target node
if(root==target)
{
// print all nodes below target node which are at distance k
PrintAtLevelK(target,k);
return 0;
}
// next step- ancestor
// if target node lies in left subtree
int DL=PrintAtDistanceK(root->left,target,k);
if(DL!=-1)
{
// Again there are two cases
//1) Ancestor itself or need to go right ancestor
if(DL+1==k)
{
cout<<root->data<<" ";
}
else
{
// search nodes in right ancestor which are at distance DL
PrintAtLevelK(root->right,k-2-DL);
}
return DL+1;
}
// similarly if target node lies in right subtree
int DR=PrintAtDistanceK(root->right,target,k);
if(DR!=-1)
{
// Again there are two cases
//1) Ancestor itself or need to go left ancestor
if(DR+1==k)
{
cout<<root->data<<" ";
}
else
{
// search nodes in left ancestor which are at distance DL
PrintAtLevelK(root->left,k-2-DR);
}
return DR+1;
}
// if node was not present in left and right subtree of given tree
return -1;
}
/*
The current implementation of findNode assumes the tree is a BST, where nodes are searched based on the left subtree containing smaller values and the right subtree containing larger values.
Since the tree might not be a BST, you should modify findNode to perform a simple traversal that doesn’t rely on the BST property.
This way, it will correctly find the node in a general binary tree.
*/
/*
node* findNode(node* root, int value)
{
// Base case
if (root == NULL || root->data == value)
{
return root;
}
// Recursively search in the left subtree
if (value < root->data)
{
return findNode(root->left, value);
}
// Recursively search in the right subtree
return findNode(root->right, value);
}
*/
node* findNode(node* root, int value)
{
// Base case
if (root == NULL || root->data == value)
{
return root;
}
// Recursively search in the left subtree
node* leftResult = findNode(root->left, value);
if (leftResult != NULL)
{
return leftResult;
}
// Recursively search in the right subtree
return findNode(root->right, value);
}
int main()
{
int n;
cin >> n;
int preOrder[10000], inOrder[10000];
for (int i = 0; i < n; i++)
{
cin >> preOrder[i];
}
for (int i = 0; i < n; i++)
{
cin >> inOrder[i];
}
node *root = createTreeFromTrav(inOrder, preOrder, 0, n - 1);
int t,value,k;
cin>>t;
while(t--)
{
cin>>value>>k;
// Find the node corresponding to the value
node* no = findNode(root, value);
// If the node exists, print nodes at distance k
if (no != NULL)
{
PrintAtDistanceK(root, no, k);
}
else
{
cout << "0";
}
cout<<endl;
}
return 0;
}
My 2 testcases are getting failed ? what’s the problem in above code ?