Easy || 0 ms || 100% || Fully Explained || (Java, C++, Python, JS, C, Python3)
We need to keep track of two things:\n\t1. Any node should have smaller elements as left children and vice versa for right children... \n\t2. The BST should be 
We need to keep track of two things:\n\t**1. Any node should have smaller elements as left children and vice versa for right children... \n\t2. The BST should be Height Balanced...**\nNote, 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...\n# **Java Solution:**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Convert Sorted Array to Binary Search Tree.\nMemory Usage: 43 MB, less than 82.54% of Java online submissions for Convert Sorted Array to Binary Search Tree.\n```\nclass Solution {\n    public TreeNode sortedArrayToBST(int[] nums) {\n        // Base condition...\n        if (nums.length == 0)\n\t\t\treturn null;\n        // Call the function recursively...\n\t\treturn sortedArrayToBST(nums, 0, nums.length - 1);\n\t}\n    // Create another function which will convert any particular range of given nums array...\n    // & return its corresponding BST root node....\n\tpublic TreeNode sortedArrayToBST(int[] nums, int beg, int end) {\n        // If beg > end, return NULL, as we receive a wrong range...\n\t\tif (beg > end)\n\t\t\treturn null;\n        // set the middle node...\n\t\tint mid = (beg + end) / 2;\n        // Initialise root node with value same as nums[mid]...\n\t\tTreeNode root = new TreeNode(nums[mid]);\n        // Assign left subtrees as the same function called on left subranges...\n\t\troot.left = sortedArrayToBST(nums, beg, mid - 1);\n        // Assign right subtrees as the same function called on right subranges...\n\t\troot.right = sortedArrayToBST(nums, mid + 1, end);\n        // Return the root node...\n\t\treturn root;\n    }\n}\n```\n\n# **C++ Solution:**\nRuntime: 5 ms, faster than 89.56% of C++ online submissions for Convert Sorted Array to Binary Search Tree.\nMemory Usage: 13.1 MB, less than 87.25% of C++ online submissions for Convert Sorted Array to Binary Search Tree.\n```\nclass Solution {\npublic:\n    TreeNode* sortedArrayToBST(vector<int>& nums) {\n        // Base condition...\n        if (nums.size() == 0)\n\t\t\treturn NULL;\n        // Call the function recursively...\n\t\treturn sortedArrayToBST(nums, 0, nums.size() - 1);\n    }\n    // Create another function which will convert any particular range of given nums array...\n    // & return its corresponding BST root node....\n\tTreeNode* sortedArrayToBST(vector<int>& nums, int beg, int end) {\n        // If beg > end, return NULL, as we receive a wrong range...\n\t\tif (beg > end)\n\t\t\treturn NULL;\n        // If beg == end, return a new node having value same as nums[beg]... \n        if(beg == end)\n            return new TreeNode(nums[beg]);\n        // set the middle node...\n\t\tint mid = (beg + end) / 2;\n        // Initialise root node with value same as nums[mid]\n\t\tTreeNode* root = new TreeNode(nums[mid]);\n        // Assign left subtrees as the same function called on left subranges...\n\t\troot->left = sortedArrayToBST(nums, beg, mid - 1);\n        // Assign right subtrees as the same function called on right subranges...\n\t\troot->right = sortedArrayToBST(nums, mid + 1, end);\n        // Return the root node\n\t\treturn root;\n    }\n};\n```\n\n# **Python Solution:**\n```\nclass Solution(object):\n    def sortedArrayToBST(self, nums):\n        # Base condition...\n        if len(nums) == 0:\n            return None\n        # set the middle node...\n        mid = len(nums)//2\n        # Initialise root node with value same as nums[mid]\n        root = TreeNode(nums[mid])\n        # Assign left subtrees as the same function called on left subranges...\n        root.left = self.sortedArrayToBST(nums[:mid])\n        # Assign right subtrees as the same function called on right subranges...\n        root.right = self.sortedArrayToBST(nums[mid+1:])\n        # Return the root node...\n        return root\n```\n        \n# **JavaScript Solution:**\n```\nvar sortedArrayToBST = function(nums) {\n    // Call the function recursively...\n\treturn ConvToBST(nums, 0, nums.length - 1);\n}\n// Create a function which will convert any particular range of given nums array...\n// & return its corresponding BST root node....\nvar ConvToBST = function(nums, beg, end) {\n    // If beg > end, return NULL, as we receive a wrong range...\n\tif (beg > end)\n\t\treturn null;\n    // set the middle node...\n\tvar mid = Math.ceil((beg + end) / 2);\n    // Initialise root node with value same as nums[mid]...\n\tvar root = new TreeNode(nums[mid]);\n    // Assign left subtrees as the same function called on left subranges...\n\troot.left = ConvToBST(nums, beg, mid - 1);\n    // Assign right subtrees as the same function called on right subranges...\n\troot.right = ConvToBST(nums, mid + 1, end);\n    // Return the root node...\n\treturn root;\n};\n```\n\n# **C Language:**\n```\nstruct TreeNode* ConvToBST(int *nums, int beg, int end){\n    if(end < beg)\n        return NULL ;\n    int mid = (beg + end)/2 ;\n    struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));\n    root->val = nums[mid];\n    root->left = ConvToBST(nums, beg, mid-1);\n    root->right = ConvToBST(nums, mid+1, end);\n    return root;\n}\nstruct TreeNode* sortedArrayToBST(int* nums, int numsSize){\n    if(numsSize <= 0)\n        return NULL;\n    else\n        return ConvToBST(nums, 0, numsSize-1);\n}\n```\n\n# **Python3 Solution:**\n```\nclass Solution:\n    def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:\n        # Base condition...\n        if len(nums) == 0:\n            return None\n        # set the middle node...\n        mid = len(nums)//2\n        # Initialise root node with value same as nums[mid]\n        root = TreeNode(nums[mid])\n        # Assign left subtrees as the same function called on left subranges...\n        root.left = self.sortedArrayToBST(nums[:mid])\n        # Assign right subtrees as the same function called on right subranges...\n        root.right = self.sortedArrayToBST(nums[mid+1:])\n        # Return the root node...\n        return root\n```\n**I am working hard for you guys...\nPlease upvote if you find any help with this code...**
['Recursion', 'C', 'Python', 'Java', 'Python3', 'JavaScript']