Balance a Binary Search Tree - Problem
Balance a Binary Search Tree
You're given the root of a binary search tree (BST) that may be unbalanced. Your task is to transform it into a height-balanced BST while preserving all the original node values and the BST property.
A BST is considered balanced if for every node, the height difference between its left and right subtrees is at most 1. This ensures optimal search performance with O(log n) operations.
Input: Root of a binary search tree
Output: Root of a balanced binary search tree with the same values
Note: If multiple balanced BSTs are possible, you can return any valid one.
You're given the root of a binary search tree (BST) that may be unbalanced. Your task is to transform it into a height-balanced BST while preserving all the original node values and the BST property.
A BST is considered balanced if for every node, the height difference between its left and right subtrees is at most 1. This ensures optimal search performance with O(log n) operations.
Input: Root of a binary search tree
Output: Root of a balanced binary search tree with the same values
Note: If multiple balanced BSTs are possible, you can return any valid one.
Example: [1,null,2,null,3,null,4] โ [2,1,3,null,null,null,4] Input & Output
example_1.py โ Basic Unbalanced BST
$
Input:
root = [1,null,2,null,3,null,4,null,null]
โบ
Output:
[2,1,3,null,null,null,4] or [3,1,4,null,2] or other balanced arrangements
๐ก Note:
The input is a completely right-skewed BST. After balancing, we get a tree where each node's subtrees differ by at most 1 in height. Multiple valid balanced arrangements exist.
example_2.py โ Left-skewed BST
$
Input:
root = [4,3,null,2,null,1]
โบ
Output:
[2,1,3,null,null,null,4] or other balanced arrangements
๐ก Note:
The input is left-skewed. The balanced version has node 2 or 3 as root (depending on implementation), with optimal height distribution.
example_3.py โ Already Balanced
$
Input:
root = [2,1,3]
โบ
Output:
[2,1,3]
๐ก Note:
The tree is already balanced, so we can return it as-is or any other balanced arrangement with the same values.
Visualization
Tap to expand
Understanding the Visualization
1
Current Structure Analysis
The company has a chain-of-command problem - too many management levels in a straight line
2
Employee Enumeration
List all employees by seniority level (equivalent to in-order traversal)
3
Choose Middle Management
Select the middle-ranked person as the department head
4
Divide Teams
Split remaining employees into two balanced sub-teams
5
Recursive Organization
Repeat the process for each sub-team to create perfect balance
Key Takeaway
๐ฏ Key Insight: An in-order traversal of a BST produces a sorted array, which can be used to construct a perfectly balanced BST using divide-and-conquer in O(n) time.
Time & Space Complexity
Time Complexity
O(n! ร n)
Factorial time to generate all possible BST structures, times n to check balance
โ Quadratic Growth
Space Complexity
O(n)
Space for recursion stack and storing node values
โก Linearithmic Space
Constraints
- The number of nodes in the tree is in the range [1, 104]
- 1 โค Node.val โค 105
- All node values are unique
- The input tree is guaranteed to be a valid BST
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code