C++ and Python3 || Divide & Conquer(Recursion) || Simple and Optimal
\n\n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(log n)\n Add your space complexity here, e.g. O(n
> ![image.png](https://assets.leetcode.com/users/images/debbff59-5dcd-4af5-a7dc-3ccf55aeb3f6_1708707197.9381366.png)\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(log n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    TreeNode* helper(vector<int> &nums, int l, int r) {\n        if(l > r) return NULL;\n        int mid = (l + r) / 2;\n        TreeNode* root = new TreeNode(nums[mid]);\n        root->left = helper(nums, l, mid - 1);\n        root->right = helper(nums, mid + 1, r);\n        return root;\n    }\n\n    TreeNode* sortedArrayToBST(vector<int>& nums) {\n        return helper(nums, 0, nums.size() - 1);\n    }\n};\n```\n```Python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:\n        def helper(l, r):\n            if l > r:\n                return None\n            mid = (l + r) // 2\n            root = TreeNode(nums[mid])\n            root.left = helper(l, mid - 1)\n            root.right = helper(mid + 1, r)\n            return root\n        return helper(0, len(nums) - 1)\n```
['Divide and Conquer', 'Tree', 'Binary Search Tree', 'Recursion', 'C++', 'Python3']