💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\n\n\n\n\njavascript []\n//JavaScript COde\n/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n *     this.val
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/6d1af61f-2e6c-43f9-b2a7-b3740b62d31c_1722912411.3303776.jpeg)\n![Screenshot 2024-08-06 081532.png](https://assets.leetcode.com/users/images/71272c58-58f1-43f7-9cc1-fda586278394_1722912419.6489208.png)\n![Screenshot 2024-08-06 081023.png](https://assets.leetcode.com/users/images/2c243c9b-dc33-4391-8a93-59936918f6aa_1722912426.410354.png)\n![Screenshot 2024-08-06 080322.png](https://assets.leetcode.com/users/images/927e3f73-9595-4296-950d-0ad73bdba092_1722912433.4331672.png)\n![Screenshot 2024-08-06 080220.png](https://assets.leetcode.com/users/images/2a214328-e1b2-44a6-a9be-3d350bff5b47_1722912440.291036.png)\n![Screenshot 2024-08-06 075921.png](https://assets.leetcode.com/users/images/0da537b0-70bf-4adb-a519-210419e0b733_1722912447.4080994.png)\n![Screenshot 2024-08-06 075441.png](https://assets.leetcode.com/users/images/322e66e3-baa4-4b39-91fb-d4d36a6f8adb_1722912454.475238.png)\n```javascript []\n//JavaScript COde\n/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.left = (left===undefined ? null : left)\n *     this.right = (right===undefined ? null : right)\n * }\n */\n/**\n * @param {number[]} nums\n * @return {TreeNode}\n */\n\nvar sortedArrayToBST = function(nums, left = 0, right = nums.length - 1) {\n    if (left > right) \n        return null;\n    \n    const mid = Math.floor((left + right) / 2);\n    const node = new TreeNode(nums[mid]);\n    \n    node.left = sortedArrayToBST(nums, left, mid - 1);\n    node.right = sortedArrayToBST(nums, mid + 1, right);\n    \n    return node;\n}\n```\n```C++ []\n//C++ Code\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),\n * right(right) {}\n * };\n */\nclass Solution {\n    TreeNode* helper(vector<int>& arr, int s, int e) {\n        if (s > e)\n            return nullptr;\n        int mid = s + (e - s) / 2 ;\n        TreeNode* node = new TreeNode(arr[mid]);\n        node->left = helper(arr, s, mid - 1);\n        node->right = helper(arr, mid + 1, e);\n        return node;\n    }\n\npublic:\n    TreeNode* sortedArrayToBST(vector<int>& arr) {\n        int n = arr.size();\n        if (n == 0)\n            return nullptr;\n        TreeNode* root = helper(arr, 0, n - 1);\n        return root;\n    }\n};\n```\n```Python3 []\n#Python3 Code\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(arr, s, e):\n            if s > e:\n                return None\n            mid = s + (e - s) // 2\n            node = TreeNode(arr[mid])\n            node.left = helper(arr, s, mid - 1)\n            node.right = helper(arr, mid + 1, e)\n            return node\n        \n        n = len(nums)\n        if n == 0:\n            return None\n        return helper(nums, 0, n - 1)\n```\n```Java []\n//Java Code\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n\n    private TreeNode helper(int[] nums, int s, int e){\n        if(s > e)\n            return null;\n        int mid = s + (e - s) / 2;\n        TreeNode node = new TreeNode(nums[mid]);\n        node.left = helper(nums, s, mid - 1);\n        node.right = helper(nums, mid + 1, e);\n        return node;\n    }\n    public TreeNode sortedArrayToBST(int[] nums) {\n        int n = nums.length;\n        if(n == 0)\n            return null;\n        return helper(nums, 0, n - 1);\n    }\n}\n```\n```C []\n//C Code\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n */\nstruct TreeNode* createNewNode(int val){\n    struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));\n    newNode->val = val;\n    newNode->left = NULL;\n    newNode->right = NULL;\n    return newNode;\n}\nstruct TreeNode* helper(int* nums, int s, int e){\n    if(s > e)\n        return NULL;\n    int mid = s + (e - s) / 2;\n    struct TreeNode* node = createNewNode(nums[mid]);\n    node->left = helper(nums, s, mid - 1);\n    node->right = helper(nums, mid + 1, e);\n    return node;\n}\nstruct TreeNode* sortedArrayToBST(int* nums, int numsSize) {\n    return helper(nums, 0, numsSize - 1);\n}\n```\n```Python []\n#Python Code\n# Definition for a binary tree node.\n# class TreeNode(object):\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution(object):\n    def sortedArrayToBST(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: TreeNode\n        """\n        def helper(arr, s, e):\n            if s > e:\n                return None\n            mid = s + (e - s) // 2\n            node = TreeNode(arr[mid])\n            node.left = helper(arr, s, mid - 1)\n            node.right = helper(arr, mid + 1, e)\n            return node\n        \n        n = len(nums)\n        if n == 0:\n            return None\n        return helper(nums, 0, n - 1)\n```\n```C# []\n//C# Code\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     public int val;\n *     public TreeNode left;\n *     public TreeNode right;\n *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\npublic class Solution {\n    private TreeNode helper(int[] nums, int s, int e){\n        if(s > e)\n            return null;\n        int mid = s + (e - s) / 2;\n        TreeNode node = new TreeNode(nums[mid]);\n        node.left = helper(nums, s, mid - 1);\n        node.right = helper(nums, mid + 1, e);\n        return node;\n    }\n    \n    public TreeNode SortedArrayToBST(int[] nums) {\n        return helper(nums, 0, nums.Length - 1);\n    }\n}\n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n![th.jpeg](https://assets.leetcode.com/users/images/7ebd13c9-a074-42f6-9d81-836b495710df_1722912683.6856997.jpeg)\n
['Array', 'Tree', 'Binary Search Tree', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']