O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022
\n\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\nTake care broth
```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* ***Java***\n```\npublic TreeNode sortedArrayToBST(int[] num) {\n    if (num.length == 0) {\n        return null;\n    }\n    TreeNode head = helper(num, 0, num.length - 1);\n    return head;\n}\n\npublic TreeNode helper(int[] num, int low, int high) {\n    if (low > high) { // Done\n        return null;\n    }\n    int mid = (low + high) / 2;\n    TreeNode node = new TreeNode(num[mid]);\n    node.left = helper(num, low, mid - 1);\n    node.right = helper(num, mid + 1, high);\n    return node;\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 7.0MB*** (beats 100.00% / 100.00%).\n* ***C++***\n```\nclass Solution {\npublic:\n    TreeNode* sortedArrayToBST(vector<int>& nums) {\n        return helper(nums, 0, nums.size()-1);\n    }\n    TreeNode* helper(vector<int>& nums, int low,int high){\n        if(low<=high){\n            int mid = low + (high-low)/2;\n            TreeNode* root = new TreeNode(nums[mid]);\n            root->left = helper(nums, low, mid-1);\n            root->right = helper(nums, mid+1, high);\n            return root;\n        }\n        return NULL;\n    }\n};\n```\n\n```\n```\n\n```\n```\n\n\nThe best result for the code below is ***26ms / 12.2MB*** (beats 95.42% / 82.32%).\n* ***Python***\n```\n# Definition for a  binary tree node\n# class TreeNode:\n#     def __init__(self, x):\n#         self.val = x\n#         self.left = None\n#         self.right = None\n\nclass Solution:\n    # @param num, a list of integers\n    # @return a tree node\n    # 12:37\n    def sortedArrayToBST(self, num):\n        if not num:\n            return None\n\n        mid = len(num) // 2\n\n        root = TreeNode(num[mid])\n        root.left = self.sortedArrayToBST(num[:mid])\n        root.right = self.sortedArrayToBST(num[mid+1:])\n\n        return root\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***51ms / 34.2MB*** (beats 100.00% / 84.12%).\n* ***JavaScript***\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 */\nvar sortedArrayToBST = function(nums) {\n    if(nums.length === 0 || null === nums){\n        return null;\n    }\n    let mid = Math.floor(nums.length/2);\n    let root = new TreeNode(nums[mid]);\n    root.left = sortedArrayToBST(nums.slice(0, mid));\n    root.right = sortedArrayToBST(nums.slice(mid+1, nums.length));\n    return root;\n};\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***68ms / 44.2MB*** (beats 100.00% / 45.25%).\n* ***Kotlin***\n```\nfun main() {\n  val node = sortedArrayToBST(intArrayOf(-10,-3,0,5,9))\n}\n\nfun sortedArrayToBST(nums: IntArray): TreeNode? {\n  return buildTree(nums,0,nums.size-1)\n}\nfun buildTree(nums: IntArray,start:Int,end:Int):TreeNode?{\n\n    if(start>end) return null\n    val mid = (start+end)/2\n    val node = TreeNode(nums[mid])\n    node.left = buildTree(nums,start,mid-1)\n    node.right = buildTree(nums,mid+1,end)\n    return node\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***12ms / 32.2MB*** (beats 95% / 84%).\n* ***Swift***\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     public var val: Int\n *     public var left: TreeNode?\n *     public var right: TreeNode?\n *     public init() { self.val = 0; self.left = nil; self.right = nil; }\n *     public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }\n *     public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {\n *         self.val = val\n *         self.left = left\n *         self.right = right\n *     }\n * }\n */\n\n// Strategy:\n// * probably some form of recursion\n// * recursion returns the head\n// * base case: \n// * if array only has one value, return that value\n// * if array is empty, return nil\n\nclass Solution {\n    func sortedArrayToBST(_ nums: [Int]) -> TreeNode? {\n        func createBSTRecursive(_ leftIdx: Int, _ rightIdx: Int) -> TreeNode? {\n            if leftIdx > rightIdx {\n                return nil\n            }\n            if leftIdx == rightIdx {\n                return TreeNode(nums[leftIdx])\n            }\n            \n            let middleIdx = (leftIdx + rightIdx) / 2\n            let val = nums[middleIdx]\n            \n            let tree = TreeNode(val)\n            tree.left = createBSTRecursive(leftIdx, middleIdx - 1)\n            tree.right = createBSTRecursive(middleIdx + 1, rightIdx)\n            \n            return tree\n        }\n        \n        return createBSTRecursive(0, nums.count - 1)\n    }\n}\n```\n\n```\n```\n\n```\n```\n\n***"Open your eyes. Expect us." - \uD835\uDCD0\uD835\uDCF7\uD835\uDCF8\uD835\uDCF7\uD835\uDD02\uD835\uDCF6\uD835\uDCF8\uD835\uDCFE\uD835\uDCFC***\n
['Recursion', 'Swift', 'C', 'PHP', 'Python', 'Java', 'Python3', 'Kotlin', 'JavaScript']