posted 1 week ago
This question is:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
One example is with this input array, [-10,-3,0,5,9] , a valid tree would be
0
/ \
-10 5
\ \
-3. 9
This is the code that solves this problem:
I am confused how this exactly works. The output with the above example is [0,-10,5,null,-3,null,9].
I am confused on what each step of recursion does here and when left would ever be greater than right? Could someone walk through the steps on what the left, right, and midpoint would be at each step of this function? I am confused at which points the right recursive call is used after the left.
I haven't been able to fully understand recursion and this would help a ton!
Thank you so much any help is appreciated.