Last Updated: 18 Nov, 2021

Counts the Nodes

Easy
Asked in companies
AmazonGoogle incD.E.Shaw

Problem statement

You are appointed a critical role to infiltrate the enemy base and steal the information. The enemy base is in the form of a BST and has ‘N’ rooms, where each room denotes a node in BST. All the rooms have a distinct number assigned, say ‘X’. Your task is to raid all the rooms whose number lies in the range [L, R]. After the raid, you have to report the number of rooms you raided.

A BST is defined as follows:

The left subtree of a node contains only nodes with keys less than or equal to the node's key.
The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
Both the left and right subtrees must also be binary search trees.

                         Example of a BST

Input Format-
First-line contains ‘T’, denoting the number of Test cases.
For each Test case:
The first line contains an integer ‘N’, denoting the number of nodes in the BST.
The following line will contain the values of the tree’s nodes in the level order form ( -1 for 'NULL' node). Refer to the example for further clarification.
The following line contains two space-separated integers, ‘L’ and ‘R’.

The input of the tree depicted in the image above will be like :

6 → represents the total number of nodes.
7 2 9 1 5 -1 14 -1 -1 -1 -1 -1 -1 → represents the level order of Tree.
Explanation :
Level 1 :
The root node of the tree is 7.

Level 2 :
The left child of 7 = 2.
The right child of 7 = 9.

Level 3 :
The left child of 2 = 1.
The right child of 7 = 5.
The left child of 9 = null (-1).
The right child of 9 = 14.

Level 4:
The left child of 1 = null (-1).
The right child of 1 = null (-1).
The left child of 5 = null (-1).
The right child of 5 = null (-1).
The left child of 14 = null (-1).
The right child of 14 = null (-1).
Output Format-
For each test case, print an integer denoting the number of rooms you raided.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints -
1 <= ‘T’ <= 5
1 <= ‘N’ <= 10^5
1 <= ‘X’ <= 10^5, for 1 <= i <= ‘N’
1 <= ‘L’ <= ‘R’ <= 10^5 
Note- the sum of ‘N’ over all test cases does not exceed 10^5.

Time Limit: 1 sec

Approaches

01 Approach

Approach: We will traverse the given tree. For each node, we will check if the node’s number lies in the given range [L, R].

 

Algorithm:

  • Func dfs(root, ans,  L, R).
    • If ‘root’ is ‘Null’.
      • Return.
    • If the root satisfies the condition.
      • Increment ‘ans’ by 1.
    • Call the func dfs(root -> left, ans, L, R).
    • Call the func dfs(root -> right, ans, L, R).

 

  • Func Main().
    • Create a variable ‘ans’ and initialize it to 0.
    • Call the function dfs.
    • Print ‘ans’.

02 Approach

Approach: We will traverse the given tree. For each node, we will check if the node’s number lies in the given range [L, R]. If yes, then add 1 to answer and recur for both of its children. If the current node is smaller than the low value of the range, then recur for the right child. Else recur for the left child.

 

Algorithm:

  • Func dfs(root, ans,  L, R).
    • If ‘root’ is ‘Null’.
      • Return.
    • If the root satisfies the condition.
      • Increment ‘ans’ by 1.
      • Call the func dfs(root -> left, ans, L, R).
      • Call the func dfs(root -> right, ans, L, R).
    • Else if the root is smaller than ‘L’.
      • Call the func dfs(root -> right, ans, L, R).
    • Else.
      • Call the func dfs(root -> left, ans, L, R).

 

  • Func Main().
    • Create a variable ‘ans’ and initialize it to 0.
    • Call the function dfs.
    • Print ‘ans’.