Given an array of integers, is there a way to quickly convert it to a binary search tree (unbalanced)? I tried to insert it one by one for each element, but that means I have to go through from the very beginning for each insert. It works fine, but I think the worst case is O (N ^ 2) for imbalance, for example. array is sorted. Given the big N, I think it will take some time.
Returning to my question, is there a way to do this faster than the algorithm that I have outlined?
For example, given an array [4,5,2,3,1], is there a quick way to create this?
4
/ \
2 5
/ \
1 3
source
share