# Count BST subtrees that lie in given range in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

We are given a Binary search tree as an input. The goal is to find the count of subtrees in BST which have node values in between the range of start and end. If start is 5 and end is 50. Then count subtrees in BST whose all nodes have weights >=5 and <=50.

Input − tree given below − range [ 3-6 ] Output − Count of tree lying in range − 2

Explanation − For nodes 4 and 6 only. Their subtrees ( NULL ) lie between 3-6.

Input − tree given below: range [ 12-20 ] Output − Count of tree lying in range − 3

Explanation − For nodes 16, 14 and 20. Their subtrees lie between 12-20.

## Approach used in the below program is as follows

• Structure Btreenode is used to create a node of a tree, with info part as integer and self referencing left and right pointers to point to subtrees.

• Function Btreenode* insert(int data) is used to create a node with data as info and left and right pointers as NULL.

• Create a BST using insert function by calling it for a given structure. To add nodes in right to the root node ( root->right = insert(70); ) and to the left of root ( root->left = insert(30); ).

• Variables l and h are used to store minimum and maximum value of a range.

• Variable count stores the count of BST’s inside the tree that are in range between l and h. Initially 0.

• Function getBtreeCount(Btreenode* root, int low, int high, int* count) takes the root of BST, left and right boundaries of range and address of count as parameters and updates the value of count for each recursive call.

• For current root check if it is NULL, if yes return 1 as it is not part of the tree.

• For current nodes, check for all its left and right subtree node’s whether they lie in the given range. By recursive calls getBtreeCount(root->left, low, high, count); and getBtreeCount(root->right, low, high, count);

• If both subtrees are lying between the range and the current node is also lying in the range then the tree rooted at the current node is in range. Increment count. if (left && right && root->info >= low && root->info <= high) and ++*count; return 1.

• At the end count will have updated value as count of all subtrees.

• Print the result in count.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
// A BST node
struct Btreenode {
int info;
Btreenode *left, *right;
};
int getBtreeCount(Btreenode* root, int low, int high, int* count){
// Base case
if (root == NULL)
return 1;
int left = getBtreeCount(root->left, low, high, count);
int right = getBtreeCount(root->right, low, high, count);
if (left && right && root->info >= low && root->info <= high) {
++*count;
return 1;
}
return 0;
}
Btreenode* insert(int data){
Btreenode* temp = new Btreenode;
temp->info = data;
temp->left = temp->right = NULL;
return (temp);
}
int main(){
/* BST for input
50
/ \
30 70
/ \ / \
20 40 60 80 */
Btreenode* root = insert(50);
root->left = insert(30);
root->right = insert(70);
root->left->left = insert(20);
root->left->right= insert(40);
root->right->left = insert(60);
root->right->right = insert(80);
int l = 10;
int h = 50;
int count=0;
getBtreeCount(root, l, h, &count);
cout << "Count of subtrees lying in range: " <<count;
return 0;
}

## Output

Count of subtrees lying in range: 3
{"ft_lang_label":"__label__en","ft_lang_prob":0.77357525,"math_prob":0.9764864,"size":4735,"snap":"2022-40-2023-06","text_gpt3_token_len":1272,"char_repetition_ratio":0.14965123,"word_repetition_ratio":0.085324235,"special_character_ratio":0.28616685,"punctuation_ratio":0.10119695,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9910867,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-04T14:18:21Z\",\"WARC-Record-ID\":\"<urn:uuid:3db212e5-f8af-4983-b111-4100b773b610>\",\"Content-Length\":\"40440\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:470c9f45-8aab-4750-9858-a6c45d779acd>\",\"WARC-Concurrent-To\":\"<urn:uuid:e4d770d5-26c4-4ba6-80f4-02ca4414a2f0>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/count-bst-subtrees-that-lie-in-given-range-in-cplusplus\",\"WARC-Payload-Digest\":\"sha1:GZPEMZRC2WW3FFOZDRSEMWMY46V5HWTP\",\"WARC-Block-Digest\":\"sha1:ENL7OWDSAOC3VQIHZO6S64NDAOJ5HL5L\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337504.21_warc_CC-MAIN-20221004121345-20221004151345-00770.warc.gz\"}"}