File tree 1 file changed +87
-0
lines changed
1 file changed +87
-0
lines changed Original file line number Diff line number Diff line change
1
+ {
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+ struct Node {
5
+ int data;
6
+ Node* right;
7
+ Node* left;
8
+
9
+ Node (int x){
10
+ data = x;
11
+ right = NULL ;
12
+ left = NULL ;
13
+ }
14
+ };
15
+ Node *insert (Node *root,int data)
16
+ {
17
+ if (root==NULL )
18
+ root=new Node (data);
19
+ else if (data<root->data )
20
+ root->left =insert (root->left ,data);
21
+ else
22
+ root->right =insert (root->right ,data);
23
+
24
+ return root;
25
+ }
26
+ // Position this line where user code will be pasted.
27
+ int main () {
28
+
29
+ int testcases;
30
+ cin>>testcases;
31
+ while (testcases--)
32
+ {
33
+ Node *root=NULL ;
34
+ int sizeOfArray;
35
+ cin>>sizeOfArray;
36
+ int arr[sizeOfArray];
37
+
38
+ for (int i=0 ;i<sizeOfArray;i++)
39
+ cin>>arr[i];
40
+
41
+ for (int i=0 ;i<sizeOfArray;i++)
42
+ {
43
+ root=insert (root,arr[i]);
44
+ }
45
+ int l,h;
46
+ cin>>l>>h;
47
+ cout<<getCountOfNode (root,l,h)<<endl;
48
+
49
+
50
+ }
51
+ return 0 ;
52
+ }
53
+ }
54
+ /* This is a function problem.You only need to complete the function given below*/
55
+ /*
56
+ The structure of a BST node is as follows:
57
+ struct Node {
58
+ int data;
59
+ Node* right;
60
+ Node* left;
61
+
62
+ Node(int x){
63
+ data = x;
64
+ right = NULL;
65
+ left = NULL;
66
+ }
67
+ };
68
+ */
69
+ int getCountOfNode (Node *root, int l, int h)
70
+ {
71
+ // your code goes here
72
+ if (!root) return 0 ;
73
+ queue<Node*> q;
74
+ int count = 0 ;
75
+ q.push (root);
76
+ while (!q.empty ()){
77
+ Node *front = q.front ();
78
+ q.pop ();
79
+ if (front->data >= l && front->data <= h)
80
+ count++;
81
+ if (front->left )
82
+ q.push (front->left );
83
+ if (front->right )
84
+ q.push (front->right );
85
+ }
86
+ return count;
87
+ }
You can’t perform that action at this time.
0 commit comments