Convert Sorted Array to Binary Search Tree
Last updated
Last updated
Given an integer array nums
where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one.
Example 1:
Since left subtree need to be smaller than root and right subtree need to be larger than root, use left half and right half with regard to the root (mid index) to fill the tree
For example, mid index value = 0, -10 & 3 need to be in left sub tree and 5 & 9 need to be in right sub tree. Therefore, fill left of root 0 by picking the mid index value from [-10,-3] (-10) and fill right of root 0 with mid index value from [5,9] (9)
Then fill right of root -10 with -3 and fill left of root 9 with 5