The problem. Given the root of a binary search tree, return a balanced BST with the same values — one where, for every node, the heights of the two subtrees differ by at most one. Any balanced answer is accepted.
root = [1, null, 2, null, 3, null, 4] -> [2, 1, 3, null, null, null, 4]
root = [2, 1, 3] -> [2, 1, 3] (already balanced)Members only · free account
The rest of this lesson, with 5 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson