What is the mistake in my code?
code is completely wrong
-
first of all your method of building the tree is wrong
you are sorting array and then making balanced bst which is not told to do
you have to simply take the data and insert it in bst one by one -
you are printing the data in a give range using loop which is again completely wrong
you have to print node in preorder format means do preorder traversal and then print the node
i hope you understand your mistakes
now try again
if you have any difficulties feel free to ask
@asaurabh_26 but whatever i am doing, shouldn’t the answer be the same it that?
if you could give me a test case, where answer will be different?
if you are building the wrong tree how can your output be correct??
Testcase:
1
10
21 18 2 6 14 24 12 13 3 4
5 10
your output:
# Preorder : 12 3 2 4 6 18 13 14 21 24
# Nodes within range are : 6
Correct Output:
# Preorder : 21 18 2 6 3 4 14 12 13 24
# Nodes within range are : 6
@asaurabh_26 both the pre-order traversals are binary search tree.
How do we know which one to build?
That isn’t explicitely mentioned in the question.
@priyamthakuria27
in question nothing is mentioned about how to buld tree
so use the default one that is make tree by puting node one by one in tree
you are making balanced bst by sorting the array
that is not mention in question