The Balance a Binary Search Tree interview question gives you a Binary Search Tree (BST) that might be highly unbalanced (e.g., essentially a linked list). You need to return a balanced BST with the same node values. A balanced tree is one where the depth of the two subtrees of every node never differs by more than 1. This Balance a Binary Search Tree coding problem tests tree transformation skills.
Meta and Google use this to test your understanding of BST properties and recursion. It evaluates whether you can flatten a tree into a sorted structure and then reconstruct it efficiently. It's a fundamental problem for understanding tree heights and balancing.
This utilizes the Divide and Conquer, Binary Search Tree, Depth-First Search, Binary Tree, Tree interview pattern.
Unbalanced BST: 1 -> 2 -> 3 -> 4
Remember that an in-order traversal of a BST always yields a sorted list. This "flattening" technique is a powerful tool for many BST problems, especially when the final structure needs to be balanced.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Convert BST to Greater Tree | Medium | Solve | |
| Recover Binary Search Tree | Medium | Solve | |
| Kth Smallest Element in a BST | Medium | Solve | |
| Lowest Common Ancestor of a Binary Search Tree | Medium | Solve | |
| Binary Search Tree to Greater Sum Tree | Medium | Solve |