Golang solution (262ms)


  • 0
    func sortedArrayToBST(nums []int) *TreeNode {
        return sortedArrayToBSTHelper(nums, 0, len(nums) - 1)
    }
    
    func sortedArrayToBSTHelper(nums []int, lo, hi int) *TreeNode {
        if lo > hi {
            return nil
        }
        mid := lo + (hi - lo) / 2
        left := sortedArrayToBSTHelper(nums, lo, mid - 1)
        right := sortedArrayToBSTHelper(nums, mid + 1, hi)
        return &TreeNode{Val: nums[mid], Left: left, Right: right}
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.