#include
#include
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node(int d)
{
data=d;
left=NULL;
right=NULL;
}
};
node * InsertinBST(node *root,int data)
{
if(root==NULL)
{
root=new node(data);
return root;
}
if(data<=root->data)
root->left=InsertinBST(root->left,data);
else
root->right=InsertinBST(root->right,data);
return root;
}
node * buildTree()
{
int n;
cin>>n;
int *A=new int[n];
for(int i=0;i<n;i++)
cin>>A[i];
node *root=NULL;
int i=0;
while(i<n)
{
root=InsertinBST(root,A[i]);
i++;
}
return root;
}
void Find_Keys(node *root,int min,int max)
{
if(root==NULL)
return;
if(min<=root->data && max<=root->data)
{
Find_Keys(root->left,min,max);
cout<<root->data<<" ";
}
else if(min<=root->data && max>=root->data)
{
Find_Keys(root->left,min,max);
cout<<root->data<<" ";
Find_Keys(root->right,min,max);
}
else if(min>=root->data && max>=root->data)
{
Find_Keys(root->right,min,max);
}
else
return;
}
void Preorder(node *root)
{
if(root!=NULL)
{
cout<data<<" ";
Preorder(root->left);
Preorder(root->right);
}
}
int main()
{
int t;
cin>>t;
int n,k1,k2;
while(t>0)
{
node *root=buildTree();
cin>>k1>>k2;
cout<<"# Preorder : “;
Preorder(root);
cout<<endl;
cout<<”# Nodes within range are : ";
Find_Keys(root,k1,k2);
t–;
}
}