Not only your decision is correct, but also better (in the asymptotic sense), assuming that only comparisons < or > are allowed between data elements.
Your solution involves sorting the data, which takes O (n log n) time and then inserts it into the tree in a roundabout way, which takes O (n) time for the total O (n log n) time complexity. Please note that after constructing the binary search tree, we can read all of its data in a sorted order using traversal in the order - that is, the solution to the interviewer's problem can be used to sort any given sequence of data elements.
Now suppose on the contrary, that in fact there is some algorithm that can solve the interviewer's problem in o (n log n) time, that is, with strictly better time complexity than the one you gave. Then this algorithm could be used to sort the data in strictly better than O (n log n). But we know that this is impossible - O (n log n) is the lower bound on the time needed to sort n elements, if everything that we are allowed to do with them is compared with < or > . Thus, such a better algorithm cannot exist.
Note that this binding is not performed if we assume that the input values ββare small integers limited by some constant, since then operations such as radix sorting can sort in O (n) time.
source share