Bird
Raised Fist0
Interview Prepbinary-search-treesmediumAmazonGoogle

Balance a BST

Choose your preparation mode3 modes available
📋
Problem

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.
Edge cases: Single node tree → same tree returnedAlready balanced BST → tree remains unchangedCompletely skewed tree (all nodes to one side) → balanced tree with minimal height
</>
IDE
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
    pass
class 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
}
0/9
Common Bugs to Avoid
Wrong: Skewed tree output identical to inputDid not perform balancing; returned input tree as is.Perform inorder traversal and rebuild balanced BST from sorted values.
Wrong: 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.
Wrong: Output tree structure differs for already balanced inputAlways rebuilding tree without preserving balanced structure.Ensure rebuilding balanced BST produces identical structure for balanced inputs.
Wrong: Timeout on large inputUsing exponential or quadratic time approach instead of O(n).Use inorder traversal and balanced BST construction in O(n) time.
Test Cases
t1_01basic
Input{"root":[1,null,2,null,3,null,4,null,null]}
Expected[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.

t1_02basic
Input{"root":[3,1,4,null,2]}
Expected[3,1,4,null,2]

The input BST is already balanced, so the output remains the same.

t2_01edge
Input{"root":[10]}
Expected[10]

Single node tree remains unchanged after balancing.

t2_02edge
Input{"root":[5,4,null,3,null,2,null,1]}
Expected[3,2,5,1,null,4,null,null,null,null,null]

Input is a left-skewed tree; output is balanced with minimal height.

t2_03edge
Input{"root":[2,1,3]}
Expected[2,1,3]

Input is already balanced with 3 nodes; output remains unchanged.

t3_01corner
Input{"root":[1,null,2,null,3,null,4,null,5]}
Expected[3,2,5,1,null,4,null,null,null]

Right skewed tree with 5 nodes balanced to minimal height tree.

t3_02corner
Input{"root":[10,5,15,2,7,null,20,1]}
Expected[7,2,15,1,5,null,20,null,null]

Unbalanced BST with mixed left and right skew balanced correctly.

t3_03corner
Input{"root":[8,3,10,1,6,null,14,null,null,4,7,13]}
Expected[7,3,13,1,4,10,14,null,6,null,null,null,null]

Complex BST balanced correctly, testing correct subtree construction.

t4_01performance
Input{"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]}
⏱ Performance - must finish in 2000ms

Input is a right skewed BST with 100 nodes. The solution must run in O(n) time to balance within 2 seconds.