<jupyter_start><jupyter_text>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.Example:Given the sorted array: [-10,-3,0,5,9],One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:      0     / \   -3   9   /   / -10  5   Balance binary tree: * A balanced binary tree, also referred to as a height-balanced binary tree, is defined as a binary tree in which the height of the left and right subtree of any node differ by not more than 1.* This type of binary search tree would adjust itself in order to maintian a low (logarithmic) height allowing for faster opertations such as insertions and deletionsLeaf Node: Left and right node would be nullFollowing are the conditions for a height-balanced binary tree:* Difference between the left and the right subtree for any node is not more than one* The left subtree is balanced* The right subtree is balanced![](images/balanced_subtree.jpg)  Solution* Everything that we do in a tree can be done recursively.* Element are sorted array the middle element would be the root of the array.* Left and right side of the root would be called recursively.* If left is  greater than right  then return None<jupyter_code>class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
        
def sorted_array_BST(nums):
     def helper(left, right):
        if left > right:
            return None

        # always choose left middle node as a root
        mid= (left + right) // 2

        # preorder traversal: node -> left -> right
        root = TreeNode(nums[mid])
        root.left = helper(left, mid - 1)
        root.right = helper(mid + 1, right)
        return root
        
        
print(sorted_array_BST([-10,-3,0,5,9]))<jupyter_output>None
{'fraction_non_alphanumeric': 0.06828302820385948, 'fraction_numerical': 0.014844136566056407, 'mean_word_length': 3.1604938271604937, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '82447', 'license': 'MIT', 'n_tokens_mistral': 562, 'n_tokens_neox': 537, 'n_words': 272, 'path': 'trees/convert_sorted_array_binary_search_tree.ipynb', 'repo_name': 'PRkudupu/Algo-python', 'size': '2021'}