After many insertions of already-sorted data, a search tree has degenerated into long chains. Return the root of a height-balanced search tree holding exactly the same values (at every node the two subtree heights differ by at most 1). Any balanced tree with the same values is accepted.
Examples
40
/ 25
10 / \
\ ==> 10 33
25 \
\ 40
33
Input: root = build_tree([40, 10, None, None, 25, None, 33])
Output (one valid answer): [25, 10, 33, None, None, None, 40]
Input: root = build_tree([3, 2, None, 1])
Output: [2, 1, 3]
Constraints
0 <= number of nodes <= 2000- All values are distinct integers
- O(n) time
Goals
- Extract the sorted values with an inorder traversal
- Rebuild a height-balanced tree from the middle outwards