Program-7
Binary Search Tree
Count the number of nodes in the BST that lie in the given range. In The existing function.
The values smaller than root go to the left side
The values greater and equal to the root go to the right side
Incorrect Code
Correct Code
Incorrect Code
void inorder(Node *root,int &cnt,int l,int h) { //Write your code here. } int getCount(Node *root, int l, int h) { // your code goes here int cnt=0; inorder(root,cnt,l,h); return cnt; }
Correct Code
void inorder(Node *root,int &cnt,int l,int h) { if(root==nullptr) return; inorder(root->left,cnt,l,h); if(root->data>=l && root->data<=h) cnt++; inorder(root->right,cnt,l,h); } int getCount(Node *root, int l, int h) { int cnt=0; inorder(root,cnt,l,h); return cnt; }
Login/Signup to comment