You are given the following tree.
And you are given 3 queries as follows
Q1 = [ 2 , 12 ]
Q2 = [ 10 , 50 ]
Q3 = [ 14 , 20 ]
Then the answers for them will be as follows.
Answer for Q1 = 3 i.e. nodes 5, 10 , 12.
Answer for Q2 = 6 i.e. nodes 10 , 12 , 15 , 19 , 20 , 28.
Answer for Q3 = 3 i.e. nodes 15 , 19 , 20.
It is guaranteed that the tree given will be a binary search tree.
It is guaranteed that all the node values will be distinct.
The first line of the input contains a single positive integer T, denoting the number of test cases.
The first line of each test case contains two positive integers N and Q, denoting the number of nodes in the BST and the number of queries respectively.
The second line contains the values of the nodes of the tree in the level order form ( 0 for NULL node) Refer to the example for further clarification.
The next Q lines of each test case contain two positive integers L and R as described in the problem statement.
The input of the tree depicted in the image above will be like:
4
3 6
1 0 5 7
0 2 0 0 0 0
0 0
Explanation :
7 is the number of nodes
The second line contains the value of nodes from 1 to 7.
Then the structure of the tree follows.
Level 1 :
The root node of the tree is 4
Level 2 :
Left child of 4 = 3
Right child of 4 = 6
Level 3 :
Left child of 3 = 1
Right child of 3 = null (0)
Left child of 6 = 5
Right child of 6 = 7
Level 4 :
Left child of 1 = null (0)
Right child of 1 = 2
Left child of 5 = null (0)
Right child of 5 = null (0)
Left child of 7 = null (0)
Right child of 7 = null (0)
Level 5 :
Left child of 2 = null (0)
Right child of 2 = null (0)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (0).
For each query print a single integer on a new line, denoting the number of nodes in the tree with a value between L and R.
You don't have to print anything, it has already been taken care of. Just Implement the given function.
1 <= T <= 50
1 <= N, Q <= 3000
1 <= Value in a node <= 10^9
1 <= L <= R <= 10^9
Time Limit : 1 sec
Two Sum IV - Input is a BST
Icarus and BSTCOUNT
Height of Binary Tree
Min Heap
Locked Binary Tree