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.

&nbsp;
Example 1:

Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null,9] is also accepted:



Example 2:

Input: nums = [1,3]
Output: [3,1]
Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 104
	-104 &lt;= nums[i] &lt;= 104
	nums is sorted in a strictly increasing order.


class Solution:
    def formNodes(self,nums, l,r):
        if l > r:
            return None
        else:
            mid = l+(r-l)//2
            node = TreeNode(nums[mid])
            node.left = self.formNodes(nums, l,mid-1)
            node.right = self.formNodes(nums, mid+1,r)
            return node
        
        
    def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
        return self.formNodes(nums, 0,len(nums)-1)
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums.length == 0) return null;
        var mid = nums.length / 2;
        var root = new TreeNode(nums[mid]);
        var left_array = Arrays.copyOfRange(nums, 0, mid);
        var right_array = Arrays.copyOfRange(nums, mid + 1, nums.length);
        root.left = sortedArrayToBST(left_array);
        root.right = sortedArrayToBST(right_array);
        return root;
    }
}
class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        
        int n = nums.size(); 
        if(n==0) //if no elements are present
            return NULL;
        if(n==1) //if only one element is present
            return new TreeNode(nums[0]); //create the root node with that element
        
        int mid = n/2; //finding the position of the middle element in the array
        TreeNode* root = new TreeNode(nums[mid]); //create root node with mid element
        
        vector<int> l (nums.begin(), nums.begin()+mid); //left subarray
        vector<int> r (nums.begin()+mid+1, nums.end()); //right subarray
        
        //using recursion to form the rest of the nodes :
        
        //the left nodes of the root must be smaller than root
        root->left = sortedArrayToBST(l); 
        
        //the right nodes of the root must be greater than root
        root->right = sortedArrayToBST(r);
        
        return root;
    }
};
var sortedArrayToBST = function(nums) {
    if (nums.length === 0) return null;
    // array length - 1 divided by 2
    let mid = (nums.length - 1) >> 1;
    let node = new TreeNode(nums[mid]);
    node.left = sortedArrayToBST(nums.slice(0, mid));
    node.right = sortedArrayToBST(nums.slice(mid + 1));
    return node;
};
Convert Sorted Array to Binary Search Tree