Overview
The problem asks us to balance a binary search tree (BST) such that the height of the tree is minimized. This means that for every node in the tree, the height of its left subtree and the height of its right subtree should differ by at most 1.
Approach 1: DFS + Build Balanced BST
Intuition
Since the problem only requires to return a balanced BST with the same node values, we can simply extract all node values in sorted order from given BST by using inorder traversal.Having all extracted vaules, we can then construct a completely new Balance BST.
To build a Balance BST from a sorted array, we choose the middle item of the array as root node. This maintains the property that the difference in number of nodes in left subtree and right subtree is at most 1. Then, we apply this process recursively on the left and right subarrays to build subtrees. By doing so, we can build a balanced BST with the same node values as the original BST.
Implementation
| |
Complexity Analysis
- Time complexity: $O(n)$. The cost of dfs is $O(n)$ and the cost of building new balanced tree is $O(n)$.
- Space complexity: $O(n)$. We need $O(n)$ space to store the sorted list and $O(logn)$ space for the recursion stack.
Approach 2: Inplace balancing
I am aware that algorithms exist for in-place BST balancing by rotating nodes, like Day-Stout-Warren (DSW). However, I don’t want to dive into that rabbit hole because it is rarely required in interviews, as the focus is typically on demonstrating a clear understanding of tree traversals and recursive construction. Therefore, I have opted to prioritize more practical approaches.