POTD Solutions | 15 Oct’ 23 | Normal BST to Balanced BST
Last Updated :
22 Nov, 2023
View all POTD Solutions
Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Search Trees but will also help you build up problem-solving skills.
GeeksforGeeks POTD Solutions | 15 October 2023
POTD 15 October: Normal BST to Balanced BST
Given a Binary Search Tree, modify the given BST to be balanced and have the minimum possible height. Return the balanced BST.
Example 1:
Example 2:
We recommend you to try this problem on our GeeksforGeeks Practice portal first, and maintain your streak to earn Geeksbits and other exciting prizes, before moving towards the solution.
The idea is to do the Inorder traversal of the Binary Search Tree and store the nodes in an array. The array will be sorted since the given tree is a BST. Then the middle element of the sorted array will be root of the new BST, ensuring that the left and right subtrees have nearly equal heights. The left subtree and right subtree can then be formed recursively.
Step-by-step approach:
- Traverse the BST inorder and store each element into an array arr[] (The elements of inorder traversal would always be sorted).
- Now, use this arr[] to create a balanced binary tree. To do this, find the middle element of the arr[] and make it root node.
- All the elements on the left side of the middle element will be left subtree and all the elements on the right side of the middle element will be right subtree
- Now, recursively build our left subtrees by again taking the middle element from the left side array and making it root node of left subtree
- Similarly, build right subtree
Below is the implementation of above approach:
C++
class Solution
{
public :
void storeBSTNodes(Node* root, vector<Node*> &nodes)
{
if (root == NULL)
return ;
storeBSTNodes(root->left, nodes);
nodes.push_back(root);
storeBSTNodes(root->right, nodes);
}
Node* buildTreeUtil(vector<Node*> &nodes, int start, int end)
{
if (start > end)
return NULL;
int mid = (start + end) / 2;
Node *root = nodes[mid];
root->left = buildTreeUtil(nodes, start, mid - 1);
root->right = buildTreeUtil(nodes, mid + 1, end);
return root;
}
Node* buildBalancedTree(Node* root)
{
vector<Node *> nodes;
storeBSTNodes(root, nodes);
int n = nodes.size();
return buildTreeUtil(nodes, 0, n - 1);
}
};
|
Java
class GfG
{
void storeBSTNodes(Node root, Vector<Node> nodes)
{
if (root == null )
return ;
storeBSTNodes(root.left, nodes);
nodes.add(root);
storeBSTNodes(root.right, nodes);
}
Node buildTreeUtil(Vector<Node> nodes, int start, int end)
{
if (start > end)
return null ;
int mid = (start + end) / 2 ;
Node node = nodes.get(mid);
node.left = buildTreeUtil(nodes, start, mid - 1 );
node.right = buildTreeUtil(nodes, mid + 1 , end);
return node;
}
Node buildBalancedTree(Node root)
{
Vector<Node> nodes = new Vector<Node>();
storeBSTNodes(root, nodes);
int n = nodes.size();
return buildTreeUtil(nodes, 0 , n - 1 );
}
}
|
Python3
import sys
sys.setrecursionlimit( 10 * * 6 )
class Solution:
def storeBSTNodes( self , root, nodes):
if root is None :
return
self .storeBSTNodes(root.left, nodes)
nodes.append(root)
self .storeBSTNodes(root.right, nodes)
def buildTreeUtil( self , nodes, start, end):
if start > end:
return None
mid = (start + end) / / 2
root = nodes[mid]
root.left = self .buildTreeUtil(nodes, start, mid - 1 )
root.right = self .buildTreeUtil(nodes, mid + 1 , end)
return root
def buildBalancedTree( self , root):
nodes = []
self .storeBSTNodes(root, nodes)
n = len (nodes)
return self .buildTreeUtil(nodes, 0 , n - 1 )
|
Javascript
class Solution
{
buildBalancedTree(root){
function storeBSTNodes(root, nodes) {
if (root == null ) return ;
storeBSTNodes(root.left, nodes);
nodes.push(root);
storeBSTNodes(root.right, nodes);
}
function buildTreeUtil(nodes, start, end) {
if (start > end) return null ;
let mid = Math.floor((start + end) / 2);
let root = nodes[mid];
root.left = buildTreeUtil(nodes, start, mid - 1);
root.right = buildTreeUtil(nodes, mid + 1, end);
return root;
}
let nodes = [];
storeBSTNodes(root, nodes);
let n = nodes.length;
return buildTreeUtil(nodes, 0, n - 1);
}
}
|
Time Complexity: O(N), As we traverse every node in the BST twice, where N is the number of nodes.
Auxiliary Space: O(N), As we have used a array to store all the nodes of the BST.
Share your thoughts in the comments
Please Login to comment...