Count BST nodes that lie in a given range (original) (raw)
Last Updated : 23 Jul, 2025
Given a **Binary Search Tree (BST) and a **range [l, h], the task is to **count the number of nodes in the **BST that lie in the given range.
**Examples:
**Input:
**Output: 3
**Explanation: There are three nodes in range [5, 45] = 5, 10 and 40.**Input:
**Output: 4
**Explanation: There are four nodes in range [10, 100] = 10, 40, 50 and 100.
Table of Content
- [Expected Approach] Using Recursion - O(n) Time and O(h) Space
- [Alternate Approach] Using Queue - O(n) Time and O(n) Space
[Expected Approach] Using Recursion - O(n) Time and O(h) Space
The idea is traverse the given **binary search tree starting from root. For every node check if this node lies in **range, if **yes, then add **1 to **result and recursively check for both of its children. If **current node is **smaller than **low value of range, then recur for **right child, else recur for **left child.
Follow the below steps to Implement the idea:
- Traverse the tree starting from the root.
- If root->data is equal to **high and root->data is equal to **low return 1.
- If root->data <= high** and ****root->data >= low then **return 1 + count on left of root + count on right of root.
- Else if **root->data < low return count on right of root.
- Else if **root->data > high return count on left of root.
Below is the implementation of the above approach.
C++ `
// C++ program to count BST nodes within // a given range #include<bits/stdc++.h> using namespace std;
class Node{ public: int data; Node* left, *right; Node(int x) { data = x; left = nullptr; right = nullptr; } };
// Returns count of nodes in BST in range [l, h] int getCount(Node *root, int l, int h) {
// Base case
if (root == nullptr) return 0;
// If current node is in range, then
// include it in count and recur for
// left and right children of it
if (root->data <= h && root->data >= l)
return 1 + getCount(root->left, l, h) +
getCount(root->right, l, h);
// If current node is smaller than low,
// then recur for right child
else if (root->data < l)
return getCount(root->right, l, h);
// Else recur for left child
else return getCount(root->left, l, h);}
int main() {
// Create a hard coded bst.
// 10
// / \
// 5 50
// / / \
// 1 40 100
Node *root = new Node(10);
root->left = new Node(5);
root->right = new Node(50);
root->left->left = new Node(1);
root->right->left = new Node(40);
root->right->right = new Node(100);
int l = 5;
int h = 45;
cout << getCount(root, l, h) << endl;
return 0;}
C
// C program to count BST nodes // within a given range #include <stdio.h> #include <stdlib.h>
struct Node { int data; struct Node* left; struct Node* right; };
// Returns count of nodes in BST in range [l, h] int getCount(struct Node* root, int l, int h) {
// Base case
if (root == NULL) return 0;
// If current node is in range, then
// include it in count and recur for
// left and right children of it
if (root->data <= h && root->data >= l)
return 1 + getCount(root->left, l, h) +
getCount(root->right, l, h);
// If current node is smaller than low,
// then recur for right child
else if (root->data < l)
return getCount(root->right, l, h);
// Else recur for left child
else return getCount(root->left, l, h);}
struct Node* createNode(int x) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = x; newNode->left = NULL; newNode->right = NULL; return newNode; }
int main() {
// Create a hard coded bst.
// 10
// / \
// 5 50
// / / \
// 1 40 100
struct Node* root = createNode(10);
root->left = createNode(5);
root->right = createNode(50);
root->left->left = createNode(1);
root->right->left = createNode(40);
root->right->right = createNode(100);
int l = 5;
int h = 45;
printf("%d\n", getCount(root, l, h));
return 0;}
Java
// Java program to count BST nodes // within a given range
import java.util.ArrayList;
class Node { int data; Node left, right;
Node(int x) {
data = x;
left = null;
right = null;
}}
class GfG {
// Returns count of nodes in BST in range [l, h]
static int getCount(Node root, int l, int h) {
// Base case
if (root == null) return 0;
// If current node is in range,
// include it and recur for left and right
if (root.data <= h && root.data >= l)
return 1 + getCount(root.left, l, h)
+ getCount(root.right, l, h);
// If current node is smaller
// than low, recur for right child
else if (root.data < l)
return getCount(root.right, l, h);
// Else recur for left child
else
return getCount(root.left, l, h);
}
public static void main(String[] args) {
// Create a hard coded bst.
// 10
// / \
// 5 50
// / / \
// 1 40 100
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(50);
root.left.left = new Node(1);
root.right.left = new Node(40);
root.right.right = new Node(100);
int l = 5;
int h = 45;
System.out.println(getCount(root, l, h));
}}
Python
Python program to count BST nodes within
a given range
class Node: def init(self, data): self.data = data self.left = None self.right = None
def getCount(root, l, h):
# Base case
if root is None:
return 0
# If current node is in range,
# include it and recur for left and right
if root.data <= h and root.data >= l:
return 1 + getCount(root.left, l, h) \
+ getCount(root.right, l, h)
# If current node is smaller
# than low, recur for right child
elif root.data < l:
return getCount(root.right, l, h)
# Else recur for left child
else:
return getCount(root.left, l, h)if name == "main":
# Create a hard coded bst.
# 10
# / \
# 5 50
# / / \
# 1 40 100
root = Node(10)
root.left = Node(5)
root.right = Node(50)
root.left.left = Node(1)
root.right.left = Node(40)
root.right.right = Node(100)
l = 5
h = 45
print(getCount(root, l, h))C#
// C# program to count BST nodes within // a given range using System;
class Node { public int data; public Node left, right;
public Node(int x) {
data = x;
left = null;
right = null;
}}
class GfG {
// Returns count of nodes in BST in range [l, h]
static int getCount(Node root, int l, int h) {
// Base case
if (root == null) return 0;
// If current node is in range,
// include it and recur for left and right
if (root.data <= h && root.data >= l)
return 1 + getCount(root.left, l, h) +
getCount(root.right, l, h);
// If current node is smaller
// than low, recur for right child
else if (root.data < l)
return getCount(root.right, l, h);
// Else recur for left child
else
return getCount(root.left, l, h);
}
static void Main(string[] args) {
// Create a hard coded bst.
// 10
// / \
// 5 50
// / / \
// 1 40 100
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(50);
root.left.left = new Node(1);
root.right.left = new Node(40);
root.right.right = new Node(100);
int l = 5;
int h = 45;
Console.WriteLine(getCount(root, l, h));
}}
JavaScript
// JavaScript program to count BST nodes // within a given range
class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } }
// Returns count of nodes in BST in range [l, h] function getCount(root, l, h) {
// Base case
if (root === null) return 0;
// If current node is in range, include
// it and recur for left and right
if (root.data <= h && root.data >= l)
return 1 + getCount(root.left, l, h)
+ getCount(root.right, l, h);
// If current node is smaller than
// low, recur for right child
else if (root.data < l)
return getCount(root.right, l, h);
// Else recur for left child
else
return getCount(root.left, l, h);}
// Create a hard coded bst.
// 10
// /
// 5 50
// / /
// 1 40 100
let root = new Node(10);
root.left = new Node(5);
root.right = new Node(50);
root.left.left = new Node(1);
root.right.left = new Node(40);
root.right.right = new Node(100);
let l = 5; let h = 45;
console.log(getCount(root, l, h));
`
[Alternate Approach] Using Queue - O(n) Time and O(n) Space
The idea is traverse the given binary search tree in level order manner using a **queue. For every node check if this node lies in **range, if **yes, then add 1 to **result and **push both of its children into queue. If current node is smaller than **low value of range, then push **right child, else push **left child.
Below is the implementation of the above approach:
C++ `
// C++ program to count BST nodes within a // given range #include<bits/stdc++.h> using namespace std;
class Node{ public: int data; Node* left, *right; Node(int x) { data = x; left = nullptr; right = nullptr; } };
// Returns count of nodes in BST in range [l, h] int getCount(Node *root, int l, int h) {
// Base case
if (root == nullptr) return 0;
queue<Node*> q;
q.push(root);
int ans = 0;
while (!q.empty()) {
Node* curr = q.front();
q.pop();
// If current node is in range, then
// increment the count and push the
// left and right children into queue.
if (curr->data <= h && curr->data >= l) {
ans++;
if (curr->left != nullptr)
q.push(curr->left);
if (curr->right != nullptr)
q.push(curr->right);
}
// If current node is smaller than low,
// then push right child into queue.
else if (curr->data < l) {
if (curr->right != nullptr)
q.push(curr->right);
}
// Else push left child
else {
if (curr->left != nullptr)
q.push(curr->left);
}
}
return ans;}
int main() {
// Create a hard coded bst.
// 10
// / \
// 5 50
// / / \
// 1 40 100
Node *root = new Node(10);
root->left = new Node(5);
root->right = new Node(50);
root->left->left = new Node(1);
root->right->left = new Node(40);
root->right->right = new Node(100);
int l = 5;
int h = 45;
cout << getCount(root, l, h) << endl;
return 0;}
Java
// Java program to count BST nodes // within a given range import java.util.*;
class Node { int data; Node left, right;
Node(int x) {
data = x;
left = null;
right = null;
}}
class GfG {
// Returns count of nodes in BST in range [l, h]
static int getCount(Node root, int l, int h) {
// Base case
if (root == null) return 0;
Queue<Node> q = new LinkedList<>();
q.add(root);
int ans = 0;
while (!q.isEmpty()) {
Node curr = q.poll();
// If current node is in range, then
// increment the count and push the
// left and right children into queue.
if (curr.data <= h && curr.data >= l) {
ans++;
if (curr.left != null)
q.add(curr.left);
if (curr.right != null)
q.add(curr.right);
}
// If current node is smaller than low,
// then push right child into queue.
else if (curr.data < l) {
if (curr.right != null)
q.add(curr.right);
}
// Else push left child
else {
if (curr.left != null)
q.add(curr.left);
}
}
return ans;
}
public static void main(String[] args) {
// Create a hard coded bst.
// 10
// / \
// 5 50
// / / \
// 1 40 100
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(50);
root.left.left = new Node(1);
root.right.left = new Node(40);
root.right.right = new Node(100);
int l = 5;
int h = 45;
System.out.println(getCount(root, l, h));
}}
Python
Python Program to count BST nodes
within a given range
from collections import deque
class Node: def init(self, x): self.data = x self.left = None self.right = None
Returns count of nodes in BST in
range [l, h]
def getCount(root, l, h):
# Base case
if root is None:
return 0
q = deque([root])
ans = 0
while q:
curr = q.popleft()
# If current node is in range, then
# increment the count and push the
# left and right children into queue.
if l <= curr.data <= h:
ans += 1
if curr.left is not None:
q.append(curr.left)
if curr.right is not None:
q.append(curr.right)
# If current node is smaller than low,
# then push right child into queue.
elif curr.data < l:
if curr.right is not None:
q.append(curr.right)
# Else push left child
else:
if curr.left is not None:
q.append(curr.left)
return ansif name == 'main':
# Create a hard coded bst.
# 10
# / \
# 5 50
# / / \
# 1 40 100
root = Node(10)
root.left = Node(5)
root.right = Node(50)
root.left.left = Node(1)
root.right.left = Node(40)
root.right.right = Node(100)
l = 5
h = 45
print(getCount(root, l, h))C#
// C# Program to count BST nodes within // a given range using System; using System.Collections.Generic;
class Node { public int data; public Node left, right;
public Node(int x) {
data = x;
left = null;
right = null;
}}
class GfG {
// Returns count of nodes in BST in range [l, h]
static int GetCount(Node root, int l, int h) {
// Base case
if (root == null) return 0;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
int ans = 0;
while (q.Count > 0) {
Node curr = q.Dequeue();
// If current node is in range, then
// increment the count and push the
// left and right children into queue.
if (curr.data >= l && curr.data <= h) {
ans++;
if (curr.left != null)
q.Enqueue(curr.left);
if (curr.right != null)
q.Enqueue(curr.right);
}
// If current node is smaller than low,
// then push right child into queue.
else if (curr.data < l) {
if (curr.right != null)
q.Enqueue(curr.right);
}
// Else push left child
else {
if (curr.left != null)
q.Enqueue(curr.left);
}
}
return ans;
}
static void Main(string[] args) {
// Create a hard coded bst.
// 10
// / \
// 5 50
// / / \
// 1 40 100
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(50);
root.left.left = new Node(1);
root.right.left = new Node(40);
root.right.right = new Node(100);
int l = 5;
int h = 45;
Console.WriteLine(GetCount(root, l, h));
}}
JavaScript
// JavaScript Program to count BST nodes // within a given range class Node { constructor(x) { this.data = x; this.left = null; this.right = null; } }
// Returns count of nodes in BST in range [l, h] function getCount(root, l, h) {
// Base case
if (root === null) return 0;
let q = [];
q.push(root);
let ans = 0;
while (q.length > 0) {
let curr = q.shift();
// If current node is in range, then
// increment the count and push the
// left and right children into queue.
if (curr.data >= l && curr.data <= h) {
ans++;
if (curr.left !== null)
q.push(curr.left);
if (curr.right !== null)
q.push(curr.right);
}
// If current node is smaller than low,
// then push right child into queue.
else if (curr.data < l) {
if (curr.right !== null)
q.push(curr.right);
}
// Else push left child
else {
if (curr.left !== null)
q.push(curr.left);
}
}
return ans;}
// Create a hard coded bst.
// 10
// /
// 5 50
// / / \
// 1 40 100
let root = new Node(10);
root.left = new Node(5);
root.right = new Node(50);
root.left.left = new Node(1);
root.right.left = new Node(40);
root.right.right = new Node(100);
let l = 5; let h = 45;
console.log(getCount(root, l, h));
`

