Why my golang solution always TLE?


  • 0
    N

    I try many solutions and this solution may be the fastest I have found.
    But it's also TLE. Can anyone help me or have a ac solution in Golang?
    This is my code.

    var nums *ListNode
    func help(left,right int) * TreeNode{
        if left >right{
            return nil
        }
        idx :=int(left + (right-left)/2)
        leftNode := help(left,idx-1)
        root :=new(TreeNode)
        root.Val = nums.Val
        root.Left = leftNode
        nums = nums.Next
        root.Right = help(idx +1,right)
        return root
    }
    
    func sortedListToBST(head *ListNode) *TreeNode {
    
        nums=head
        lens := 0
        for head!=nil{
            lens ++
            head = head.Next
        }
        return help(0,lens-1)
    }
    

Log in to reply
 

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