Create a balanced Binary Search Tree from a sorted array
Given a sorted integer array of length n, create a balanced Binary Search Tree using elements of the array.
Please try solving this problem before jumping on the solution
Click to learn
Subscribe for more updates
Preparing for interviews? IDeserve team is here to help.
Create your profile
Create your profile, and here is what you will get:
1: Interview practice platform.
2: Once you are ready to take the interview, IDeserve team will help you get connected to the best job opportunities.
3: Personalized mentorship from IDeserve team once your interview process has started.
Creation of profile shouldn't take more than 2 minutes.
A BST is balanced if: Height of left subtree and right subtree of root differ by at most 1. Left and right subtrees are subtrees is balanced.
Algorithm: 1. Initialize start = 0, end = length of the array - 1 2. Set mid = (start+end)/2 3. Create a tree node with mid as root (lets call it A). 4. Recursively do following steps: a). Calculate mid of left subarray and make it root of left subtree of A. b). Calculate mid of right subarray and make it root of right subtree of A.
Support us by whitelisting IDeserve in your ad-blocker.