It’s crucial that while offloading nodes to the list, we do an inorder() traversal - as that would visit first the left side nodes, then the root and finally the right side nodes.
1382 Balance a Binary Search Tree uses the fundamental knowledge that you build in this problem. I recommend solving that too while you are on this one.
Code
Python3
Go
Big O Analysis
Runtime
The runtime complexity here is O(N) since we would have to visit all nodes in the array atleast once to construct the tree.
Memory
The memory usage is O(N) since we are creating a new TreeNode for every value in the array.