 New update is available. Click here to update.

# BST queries

Posted: 18 Jan, 2021
Difficulty: Easy

## PROBLEM STATEMENT

#### You are given an arbitrary binary search tree (BST) with N nodes numbered 1 to N, and each node is associated with a value. You are also given Q queries, each of the Q queries are of the form [ L, R ], your task is to find the number of nodes in the BST which lie in the range L to R for each query.

##### For example
``````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.
``````
##### Note :
``````It is guaranteed that the tree given will be a binary search tree.
It is guaranteed that all the node values will be distinct.
``````
##### Input Format:
``````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.
``````

#### Example: Consider the binary tree ``````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).
``````
##### Output Format:
``````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.
``````
##### Note:
``````You don't have to print anything, it has already been taken care of. Just Implement the given function.
``````
##### Constraints:
``````1 <= T <= 50
1 <= N, Q <= 3000
1 <= Value in a node <= 10^9
1  <= L <= R <= 10^9
Time Limit : 1 sec
``````
SIMILAR PROBLEMS

Two Sum IV - Input is a BST

Posted: 22 Mar, 2022
Difficulty: Moderate

Icarus and BSTCOUNT

Posted: 1 Apr, 2022
Difficulty: Hard

Height of Binary Tree

Posted: 22 Apr, 2022
Difficulty: Easy

Min Heap

Posted: 5 May, 2022
Difficulty: Moderate

Locked Binary Tree

Posted: 12 May, 2022
Difficulty: Easy