MINIMUM HEIGHT BST

 Given a non-empty sorted array of distinct integers, construct a minimum height bst and return       root .

Approach : Since the array is element at the middle of array is going to be the root.

So we will try break the array in 3 parts( left array, middle element, right array).

Middle element will be the root of the array, then we will find the middle of left array it will be the root of left sub tree and similarly the middle of right array will be the root of right sub tree.


         We defined tree as None since there is no node in the tree.

Now we will be inserting nodes in the tree using minHeightBstHelper function.  

 


 

         BST is a class which inserts the node at its right place following the bst properties.


Time - O(n log n) 

Space - O(n) 

Solution - min height bst solution

Can you reduce its time complexity of O(n), do post the solution in comment.



Please leave comment for any suggestions or topic you would like to read.


 

Comments

  1. Hi Aishwary, You started a great initiative for helping students like me who seeking good resources for interview preparation. Thanks brother for giving such valuable content. You explained every question very well. Looking forward to learn more.

    ReplyDelete
    Replies
    1. Thanks for letting me know, I'll try to regularly update useful questions for interview.

      Delete

Post a Comment

If you have any doubt, Please let me know