Print BST Keys in the Given Range

I’m Sharing Two Approaches. Where am I Wrong? Both are Giving Exactly Same Output as Test Case Requires Though.
Sample Test Case:- T , n , arr[n] , k1 , k2

1

5

4 3 2 5 6

3 5


this code is the better one, you have to tackle it this way only, by making the bst in place and not from the sorted array

made a minor change in calling
no need to call on the subtree if their root is already out of range

Still not Able to Submit, Showing Wrong Answer.
I Tried Printing the Statement Like " #Nodes within range are " and also Without Printing These Words by Just Printing Output, Nothing is Working.


u can try submitting this and refer to this

1 Like

Hi, Seemant, I Found My Mistake.
Actually If I Run for All the 6 Test Cases Individually, it Gives Correct Output.
But If I Run for All Test Cases, My First Input’s root is appended to 2nd , 2nd root appended to 3rd and so on, so, it’s problem with my root not getting re-created for each test case.
I Must Say, You’re One of the Best TA’s I’ve Come Across in Last 4.5 Months.
Thanks a Lot.
Make Sure Prateek Sir Sees My This Reply, Because Some TA’s Intentionally Reply at 4 am or 5 am so that We Don’t ask Questions Back but You Had Been Replying Me Sincerely and Regularly, Thank You Very Much.
You Can Close This Doubt, I’m Absolutely Satisfied With Your Constant Replies :slight_smile:
I’ve Found How to Create the Root New Each Time for All Test Cases, Thanks Again :slight_smile:

Forgot to Mention What Was My Exact Mistake.
I Passed array from main() and Since arrays are passed by reference, first test case array elements were getting appended to next ones, but you passed root in buildBST(…) from main() and never used any array, so your test case elements were never merged.
I Hope it helps you if anyone faces same mistake next time.
Send Me the Link Where I Can Give You Feedback, You Deserve 5 out 5 due to Your Constant Replies :slight_smile:

hey, u can close the doubt on your end and leave a rating, thank you

1 Like