Given a BST and a range a b count how many elements are insi

Given a BST and a range [a, b], count how many elements are inside that range.

Solution

#include <iostream>
using namespace std;

struct node
{
int data;
node *left;
node *right;
};

int countNodes(node *bst,int a,int b)
{
if(bst==NULL)
return 0;
  
if(bst->data>=a && bst->data<=b)
return 1+countNodes(bst->left,a,b)+countNodes(bst->right,a,b);
else
return countNodes(bst->left,a,b)+countNodes(bst->right,a,b);
}

int main() {
   // your code goes here

   return 0;
}

Given a BST and a range [a, b], count how many elements are inside that range.Solution#include <iostream> using namespace std; struct node { int data; nod

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site