Given a Binary Search Tree (BST) and a range [low, high], the task is to count the number of nodes where all the nodes under that node (or subtree rooted with that node) lie in the given range.
Examples:
Input : low = 8, high = 10
Output: 1 Explanation: There is only 1 node with the value 10 whose subtree is in the given range.
Input: low = 12, high = 20
Output: 3 Explanation: There are three nodes whose subtree is in the given range. The nodes are 12, 10 and 14
Approach:
The idea is to traverse the given Binary Search Tree (BST) in a bottom-up manner. For every node, make recursive calls for its subtrees, if subtrees are in range and the nodes are also in range, then increment the count and return true.
Below is the implementation of the above approach:
C++
// C++ program to count BST subtrees// that lie in given range#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// Recursive function to count// subtrees that lie in a rangeboolsubtreeCntRecur(Node*root,intlow,inthigh,int&ans){if(root==nullptr)returntrue;// Check for left and right subtree.boolleft=subtreeCntRecur(root->left,low,high,ans);boolright=subtreeCntRecur(root->right,low,high,ans);// If current subtree lies within range, then increment// ans count and return true.if(root->data>=low&&root->data<=high&&left&&right){ans++;returntrue;}// Else return false as this subtree // is out of range.returnfalse;}// Function to count subtress that // lie in a given rangeintsubtreeCnt(Node*root,intlow,inthigh){intans=0;subtreeCntRecur(root,low,high,ans);returnans;}intmain(){// Binary tree // 10// / \ // 5 50// / / \ // 1 40 100Node*root=newNode(10);root->left=newNode(5);root->right=newNode(50);root->left->left=newNode(1);root->right->left=newNode(40);root->right->right=newNode(100);intlow=1,high=45;cout<<subtreeCnt(root,low,high)<<endl;return0;}
Java
// Java program to count BST subtrees// that lie in given rangeclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// Recursive function to count// subtrees that lie in a rangestaticbooleansubtreeCntRecur(Noderoot,intlow,inthigh,int[]ans){if(root==null)returntrue;// Check for left and right subtree.booleanleft=subtreeCntRecur(root.left,low,high,ans);booleanright=subtreeCntRecur(root.right,low,high,ans);// If current subtree lies within range, then increment// ans count and return true.if(root.data>=low&&root.data<=high&&left&&right){ans[0]++;returntrue;}// Else return false as this subtree // is out of range.returnfalse;}// Function to count subtrees that // lie in a given rangestaticintsubtreeCnt(Noderoot,intlow,inthigh){int[]ans={0};subtreeCntRecur(root,low,high,ans);returnans[0];}publicstaticvoidmain(String[]args){// Binary tree // 10// / \// 5 50// / / \// 1 40 100Noderoot=newNode(10);root.left=newNode(5);root.right=newNode(50);root.left.left=newNode(1);root.right.left=newNode(40);root.right.right=newNode(100);intlow=1,high=45;System.out.println(subtreeCnt(root,low,high));}}
Python
# Python program to count BST subtrees# that lie in given rangeclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# Recursive function to count# subtrees that lie in a rangedefsubtreeCntRecur(root,low,high,ans):ifrootisNone:returnTrue# Check for left and right subtree.left=subtreeCntRecur(root.left,low,high,ans)right=subtreeCntRecur(root.right,low,high,ans)# If current subtree lies within range, then increment# ans count and return true.ifroot.data>=lowandroot.data<=high \
andleftandright:ans[0]+=1returnTrue# Else return false as this subtree # is out of range.returnFalse# Function to count subtrees that # lie in a given rangedefsubtreeCnt(root,low,high):ans=[0]subtreeCntRecur(root,low,high,ans)returnans[0]if__name__=="__main__":# Binary tree # 10# / \# 5 50# / / \# 1 40 100root=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)low,high=1,45print(subtreeCnt(root,low,high))
C#
// C# program to count BST subtrees// that lie in given rangeusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// Recursive function to count// subtrees that lie in a rangestaticboolsubtreeCntRecur(Noderoot,intlow,inthigh,refintans){if(root==null)returntrue;// Check for left and right subtree.boolleft=subtreeCntRecur(root.left,low,high,refans);boolright=subtreeCntRecur(root.right,low,high,refans);// If current subtree lies within range, then increment// ans count and return true.if(root.data>=low&&root.data<=high&&left&&right){ans++;returntrue;}// Else return false as this subtree // is out of range.returnfalse;}// Function to count subtrees that // lie in a given rangestaticintsubtreeCnt(Noderoot,intlow,inthigh){intans=0;subtreeCntRecur(root,low,high,refans);returnans;}staticvoidMain(string[]args){// Binary tree // 10// / \// 5 50// / / \// 1 40 100Noderoot=newNode(10);root.left=newNode(5);root.right=newNode(50);root.left.left=newNode(1);root.right.left=newNode(40);root.right.right=newNode(100);intlow=1,high=45;Console.WriteLine(subtreeCnt(root,low,high));}}
JavaScript
// JavaScript program to count BST subtrees// that lie in given rangeclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// Recursive function to count// subtrees that lie in a rangefunctionsubtreeCntRecur(root,low,high,ans){if(root===null)returntrue;// Check for left and right subtree.letleft=subtreeCntRecur(root.left,low,high,ans);letright=subtreeCntRecur(root.right,low,high,ans);// If current subtree lies within range, then increment// ans count and return true.if(root.data>=low&&root.data<=high&&left&&right){ans[0]++;returntrue;}// Else return false as this subtree // is out of range.returnfalse;}// Function to count subtrees that // lie in a given rangefunctionsubtreeCnt(root,low,high){letans=[0];subtreeCntRecur(root,low,high,ans);returnans[0];}// Binary tree // 10// / \// 5 50// / / \// 1 40 100letroot=newNode(10);root.left=newNode(5);root.right=newNode(50);root.left.left=newNode(1);root.right.left=newNode(40);root.right.right=newNode(100);letlow=1,high=45;console.log(subtreeCnt(root,low,high));
Output
3
Time Complexity: O(n), where n is the number of nodes in the tree. Auxiliary Space: O(h), where h is the height of the tree.