Q) Print BST keys .It is showing the correct answer when testing sample input but giving wrong answer after submitting
Hey this is the correct logical implementation of print between function
void print_between_range(node*root,int k1,int k2)
{
if(root==NULL) return;
if(k1data) print_between_range(root->left,k1,k2);
if(k1<=root->data&&k2>=root->data) cout<data<<" ";
if(k2>root->data) print_between_range(root->right,k1,k2);
}
Its not necessary that k1 & k2 are present in the tree also if we find either of them say k1 then its right subtree will contain elements >k1 and we dont have to return as you have done in your code.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.