Balance a BST
Imagine you have a binary search tree that has become skewed after many insertions and deletions, causing inefficient operations. Your task is to rebalance it to restore optimal search times.
Given the root of a binary search tree (BST), return a balanced BST with the same node values. A balanced BST is defined as a binary tree in which the depth of the two subtrees of every node never differs by more than 1.
The number of nodes in the tree is in the range [1, 10^5].Node values are unique integers.The input tree is a valid BST.def balanceBST(root: Optional[TreeNode]) -> Optional[TreeNode]:public TreeNode balanceBST(TreeNode root)TreeNode* balanceBST(TreeNode* root)function balanceBST(root)def balanceBST(root: Optional[TreeNode]) -> Optional[TreeNode]:
# Write your solution here
passclass Solution {
public TreeNode balanceBST(TreeNode root) {
// Write your solution here
return null;
}
}#include <vector>
using namespace std;
TreeNode* balanceBST(TreeNode* root) {
// Write your solution here
return nullptr;
}function balanceBST(root) {
// Write your solution here
}Skewed tree output identical to inputDid not perform balancing; returned input tree as is.✅ Perform inorder traversal and rebuild balanced BST from sorted values.Balanced tree missing some nodes or duplicated nodesOff-by-one errors in recursive build causing incorrect subtree ranges.✅ Correctly handle left and right indices in recursive calls to include all nodes exactly once.Output tree structure differs for already balanced inputAlways rebuilding tree without preserving balanced structure.✅ Ensure rebuilding balanced BST produces identical structure for balanced inputs.Timeout on large inputUsing exponential or quadratic time approach instead of O(n).✅ Use inorder traversal and balanced BST construction in O(n) time.{"root":[1,null,2,null,3,null,4,null,null]}[2,1,3,null,null,null,4]The input BST is skewed to the right. After balancing, the tree root becomes 2 with left child 1 and right subtree rooted at 3 with right child 4.
{"root":[3,1,4,null,2]}[3,1,4,null,2]The input BST is already balanced, so the output remains the same.
{"root":[10]}[10]Single node tree remains unchanged after balancing.
{"root":[5,4,null,3,null,2,null,1]}[3,2,5,1,null,4,null,null,null,null,null]Input is a left-skewed tree; output is balanced with minimal height.
{"root":[2,1,3]}[2,1,3]Input is already balanced with 3 nodes; output remains unchanged.
{"root":[1,null,2,null,3,null,4,null,5]}[3,2,5,1,null,4,null,null,null]Right skewed tree with 5 nodes balanced to minimal height tree.
{"root":[10,5,15,2,7,null,20,1]}[7,2,15,1,5,null,20,null,null]Unbalanced BST with mixed left and right skew balanced correctly.
{"root":[8,3,10,1,6,null,14,null,null,4,7,13]}[7,3,13,1,4,10,14,null,6,null,null,null,null]Complex BST balanced correctly, testing correct subtree construction.
{"root":[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9,null,10,null,11,null,12,null,13,null,14,null,15,null,16,null,17,null,18,null,19,null,20,null,21,null,22,null,23,null,24,null,25,null,26,null,27,null,28,null,29,null,30,null,31,null,32,null,33,null,34,null,35,null,36,null,37,null,38,null,39,null,40,null,41,null,42,null,43,null,44,null,45,null,46,null,47,null,48,null,49,null,50,null,51,null,52,null,53,null,54,null,55,null,56,null,57,null,58,null,59,null,60,null,61,null,62,null,63,null,64,null,65,null,66,null,67,null,68,null,69,null,70,null,71,null,72,null,73,null,74,null,75,null,76,null,77,null,78,null,79,null,80,null,81,null,82,null,83,null,84,null,85,null,86,null,87,null,88,null,89,null,90,null,91,null,92,null,93,null,94,null,95,null,96,null,97,null,98,null,99,null,100]}nullInput is a right skewed BST with 100 nodes. The solution must run in O(n) time to balance within 2 seconds.
