X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Normal BST to Balanced BST
Easy Accuracy: 56.27% Submissions: 11117 Points: 2

Given a Binary Search Tree, modify the given BST such that itis balanced and has minimum possible height.

Examples :

Input:
       30
      /
     20
    /
   10
Output:
     20
   /   \
 10     30

Input:
         4
        /
       3
      /
     2
    /
   1
Output:
      3            3           2
    /  \         /  \        /  \
   1    4   OR  2    4  OR  1    3   OR ..
    \          /                   \
     2        1                     4 


Your Task:
The task is to complete the function buildBalancedTree() which takes root as the input argument, and returns the root of tree after converting the given BST into a balanced BST that has minimum possible height
. The driver code will print the height of the updated tree in output itself.
 

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Here N denotes total number of nodes in given BST.


Constraints:
1<=N<=200

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Normal BST to Balanced BST

Output Window