Count BST nodes that lie in a given range | Practice | GeeksforGeeks

Given a Binary Search Tree (BST) and a range l-h(inclusive), count the number of nodes in the BST that lie in the given range. 
The values smaller than root go to the left side
The values greater and equal to the root go to the right side
Example 1:
Input:
      10
     /  \
    5    50
   /    /  \
  1    40  100
l = 5, h = 45
Output: 3
Explanation: 5 10 40 are the node in the
range

Example 2:
Input:
     5
    /  \
   4    6
  /      \
 3        7
l = 2, h = 8
Output: 5
Explanation: All the nodes are in the
given range.

Your Task:
This is a function problem. You don't have to take input. You are required to complete the function getCountOfNode() that takes root, l ,h as parameters and returns the count.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1 <= Number of nodes <= 100
1 <= l < h < 103
0
yjayaswal1 week ago
Easiest Solution in C++
int getCount(Node *root, int l, int h){   if(!root)       return 0;   int x=getCount(root->left,l,h);   int y=getCount(root->right,l,h);   if(root->data>=l&&root->data<=h)       return x+y+1;   else       return x+y;} 
0
mayanksingh08012 weeks ago
c++ simple code
void helper(Node*root,int l,int h,int &count){   if(root==NULL)   return ;   if(root->data>=l&&root->data<=h)   count++;   helper(root->left,l,h,count);   helper(root->right,l,h,count);}int getCount(Node *root, int l, int h){ // your code goes here    if(root==NULL) return 0; int count=0; helper(root,l,h,count); return count;  } 
0
aryankhatana353 weeks ago
c++
void inorder(Node *root,vector<int>&a){   if(root==NULL){       return;   }   inorder(root->left,a);   a.push_back(root->data);   inorder(root->right,a);}int getCount(Node *root, int l, int h){ // your code goes here    vector<int> ans; inorder(root,ans); int cnt=0; for(int i=0;i<ans.size();i++){     if(ans[i]>=l && ans[i]<=h){         cnt++;     } } return cnt;
0
dipanshusharma93133 weeks ago
// java solution
class Tree{   //Function to count number of nodes in BST that lie in the given range.   int getCount(Node root,int l, int h)   {       //Your code here       if(root == null){           return 0;       }       int x = 0;      x = x + getCount(root.left, l, h);      x = x + getCount(root.right, l, h);            if(root.data >= l && root.data <= h){          return x+1;      }      else{          return x;      }   }}
0
joyrockok3 weeks ago
class Tree{static int count=0;public Tree() {    count=0;}   //Function to count number of nodes in BST that lie in the given range.   int getCount(Node root,int l, int h)   {       //Your code here         if(l <= root.data && root.data <= h) {     count++;    }        if (root.left != null) {     getCount(root.left, l, h);    }        if(root.right != null) {     getCount(root.right, l, h);    }        return count;   }}
0
gujjulassr3 weeks ago
0.1/1.3
 
class Tree{   int res=0;   //Function to count number of nodes in BST that lie in the given range.   int getCount(Node root,int l, int h)   {       //Your code here       count(root,l,h);       return res;   }   int count(Node root,int l,int h){       if(root==null){           return 0;       }       count(root.left,l,h);       count(root.right,l,h);       if(root.data>=l && root.data<=h){           res++;       }              return res;   }}
0
sachinsinghss194 weeks ago
 
op = []   def getRange(l:int, h:int, root:Node):              if root:           getRange(l, h, root.left)           if root.data >= l and root.data <= h:               op.append(root.data)           getRange(l, h, root.right)         getRange(low, high, root)   return len(op)
0
luffysama11 month ago
void inOrder(Node* root , int l , int h , int& count ){   if(root == NULL)       return;   if(root->data >= l && root->data <= h && root!=NULL){       count++;       inOrder(root->left,l,h,count);       inOrder(root->right,l,h,count);   }   if(root->data > l) }
int getCount(Node *root, int l, int h){ // your code goes here    int count = 0; inOrder(root,l,h,count); return count;} 
0
sandeep55211 month ago
7 Lines C++ Solution (Recursive)
int getCount(Node *root, int l, int h)
{
   int f=0;
   if(!root) return 0;
   if(root->data>=l and root->data<=h) f++;
   return f+getCount(root->left,l,h)+getCount(root->right,l,h);
}
0
jatinsuthar3001 month ago
Simple 5 line code: 0.2s Java Solution
class Tree
{
    //Function to count number of nodes in BST that lie in the given range.
    int getCount(Node root,int l, int h)
    {
        if(root==null) return 0;
        int count=0;
        count+=getCount(root.left, l, h);
        count+=getCount(root.right, l, h);
        return (root.data>=l && root.data<=h) 
              ? 1+count : count;
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.