Print BST elements in given range | Practice | GeeksforGeeks

Given a Binary Search Tree and a range [low, high]. Find all the numbers in the BST that lie in the given range.
Note: Element greater than or equal to root go to the right side.
Example 1:
Input:
       17
     /    \
    4     18
  /   \
 2     9 
l = 4, h = 24
Output: 4 9 17 18 

Example 2:
Input:
       16
     /    \
    7     20
  /   \
 1    10
l = 13, h = 23
Output: 16 20 

Your Task:
You don't need to read input or print anything. Your task is to complete the function printNearNodes() which takes the root Node of the BST and the range elements low and high as inputs and returns an array that contains the BST elements in the given range low to high (inclusive) in non-decreasing order.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the BST).
Constraints:
1 ≤ Number of nodes ≤ 104
1 ≤ l ≤ h ≤ 105
0
sparhibsp20182 days ago
simple c++ solution using another function
 
void fun(Node *root,int low,int high,vector<int> &v)   {       if(!root)           return ;       fun(root->left,low,high,v);       if(root->data >= low && root->data <= high )           v.push_back(root->data);       fun(root->right,low,high,v);   }   vector<int> printNearNodes(Node *root, int low, int high) {       vector<int> v;       fun(root,low,high,v);       return v;   }
 
0
kuldeepy104593 weeks ago
 void inorder(Node * root ,int low ,int high,vector<int>&ans){        if(root==NULL){            return;        }        inorder(root->left,low,high,ans);        if(root->data>=low&&root->data<=high){            ans.push_back(root->data);        }        inorder(root->right,low,high,ans);    }   vector<int> printNearNodes(Node *root, int low, int high) {       //code here          vector<int>ans;       inorder(root,low,high,ans);       return ans;          }
0
aryankhatana351 month ago
C++ sol
  void inorder(Node *root, vector<int> &a){     if(root==NULL){         return;     }     inorder(root->left,a);     a.push_back(root->data);     inorder(root->right,a); }   vector<int> printNearNodes(Node *root, int low, int high) {       //code here          vector<int> ans;       inorder(root,ans);       vector<int>temp ;       for(int i=0;i<ans.size();i++){           if(ans[i]>=low && ans[i]<=high){               temp.push_back(ans[i]);           }       }       return temp;          }
0
chhitizgoyal1 month ago
Java Solution.
 public static ArrayList<Integer> printNearNodes(Node root,int low,int high) {       ArrayList<Integer> list = new ArrayList<>();       if(root==null){           return list;       }       if(root.data>=low && root.data<=high){           list.add(root.data);       }       list.addAll(printNearNodes(root.left, low, high));       list.addAll(printNearNodes(root.right, low, high));       Collections.sort(list);       return list;   }
+1
rohanyt742 months ago
class Solution { public:  void inorder(Node*root,vector<int>&v){     if(root==NULL) return;     inorder(root->left,v);     v.push_back(root->data);     inorder(root->right,v); }   vector<int> printNearNodes(Node *root, int low, int high) {       //code here          vector<int>res;               vector<int>res1;       inorder(root,res);       for(int i=0;i<res.size();i++){           if(res[i]>=low and res[i]<=high) res1.push_back(res[i]);       }       return res1;          }};
+1
sarthakjagetiya10012 months ago
Total Time Taken:  0.2/1.4
C++
void check(Node* root, int low, int high, vector<int> &ans){
        if(root==NULL){
            return;
        }
        if((root->data >= low) && (root->data <= high)){
            ans.push_back(root->data);
        }
        check(root->left, low, high, ans);
        check(root->right, low, high, ans);
    }
    vector<int> printNearNodes(Node *root, int low, int high) {
        //code here  
        vector<int> ans;
        check(root, low, high, ans);
        sort(ans.begin(), ans.end());
        return ans;
    }
0
patildhiren442 months ago
// Java - 1.4
static ArrayList<Integer> al = new ArrayList<Integer>();
    
    static void util(Node root, int low, int high){
        if(root==null){
            return ;
        }
        
        util(root.left, low, high);
        
        if(low<=root.data && high>=root.data){
            al.add(root.data);
        }
        
        util(root.right, low, high);
    }
    
	public static ArrayList<Integer> printNearNodes(Node root,int low,int high) {
        // code here.
        al = new ArrayList<>();
        util(root,low,high);
        return al;
    }
+1
forcer3 months ago
void solve(Node *root,vector<int>&v,int l,int h) {     if(!root) return;     solve(root->left,v,l,h);     if(root->data>=l&&root->data<=h) v.push_back(root->data);     solve(root->right,v,l,h); }   vector<int> printNearNodes(Node *root, int low, int high) {       //code here          vector<int>v;       solve(root,v,low,high);       return v;          }
+1
shyamprakash8073 months ago
Simple Python SOlution :
Time Taken : 0.8/2.7
 
class Solution:   def printNearNodes(self, root, low, high):       ans = []       self.find(root, low, high, ans)       ans.sort()       return ans      def find(self, root, low, high, ans):       if root==None:           return       if low<=root.data<=high:           ans.append(root.data)       if low <= root.data and high <= root.data:           self.find(root.left,low, high, ans)       elif low >= root.data and high >= root.data:           self.find(root.right, low, high, ans)       else:           self.find(root.left, low, high, ans)           self.find(root.right, low, high, ans)
0
haritasboy12053 months ago
C++ || Recursive Solution
void solve(Node* root, int low, int high, vector<int> &ans)
    {
        if(root == NULL)
            return;
        solve(root->left,low,high,ans);
        if(root->data >= low && root->data <= high)
            ans.push_back(root->data);
        solve(root->right,low,high,ans);    
    }
    vector<int> printNearNodes(Node *root, int low, int high) {
        //code here   
        vector<int> ans;
        solve(root,low,high,ans);
        return ans;
    }
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.