You are on page 1of 2

#include <bits/stdc++.

h>
using namespace std;

struct Node {
int data;
Node* right;
Node* left;

Node(int x){
data = x;
right = NULL;
left = NULL;
}
};

Node *insert(Node *root,int data)


{
if(root==NULL)
root=new Node(data);
else if(data<root->data)
root->left=insert(root->left,data);
else
root->right=insert(root->right,data);

return root;
}

// } Driver Code Ends

/*

The structure of a BST node is as follows:

struct Node {
int data;
Node* right;
Node* left;

Node(int x){
data = x;
right = NULL;
left = NULL;
}
};
*/
void solve(Node* root, int l, int h, int &ans){
if(root==NULL){
return;
}
if(root->data >=l && root->data<=h){
ans++;
}
if(root->left)
solve(root->left, l, h, ans);
if(root->right)
solve(root->right, l, h, ans);

int getCountOfNode(Node *root, int l, int h)


{
// your code goes here
int ans=0;
solve(root, l, h, ans);
return ans;
}

// { Driver Code Starts.

int main() {

int testcases;
cin>>testcases;
while(testcases--)
{
Node *root=NULL;
int sizeOfArray;
cin>>sizeOfArray;
int arr[sizeOfArray];

for(int i=0;i<sizeOfArray;i++)
cin>>arr[i];

for(int i=0;i<sizeOfArray;i++)
{
root=insert(root,arr[i]);
}
int l,h;
cin>>l>>h;
cout<<getCountOfNode(root,l,h)<<endl;

}
return 0;
} // } Driver Code Ends

You might also like