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.

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
Binary Search Tree Balancing ProcessBefore: Unbalanced1234Height: 4In-order traversal:1234โ†‘ Middle elementAfter: Balanced3241Height: 3๐ŸŽฏ Algorithm Steps1. In-order traversal โ†’ sorted array [1,2,3,4]2. Divide & conquer โ†’ middle element (3) becomes root, recursively build left [1,2] and right [4] subtrees
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

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for recursion stack and storing node values

n
2n
โšก 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
Asked in
Facebook 45 Amazon 38 Google 32 Microsoft 28 Apple 22
89.2K Views
Medium Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen