Simple ruby got memory limit exceed


  • 0
    J
    def sorted_list_to_bst(head)
        return if head.nil?
        c = head
        len = 0
        until c.nil?
          len += 1
          c = c.next
        end
        convert_helper(head, len)
    end
    
    def convert_helper(head, len)
        return nil if len == 0
        return TreeNode.new(head.val) if len == 1
        mid = (len-1)/2
        c = head
        mid.times do
          c = c.next
        end
        root = TreeNode.new(c.val)
        root.left = convert_helper(head, mid)
        root.right = convert_helper(c.next, len-mid-1)
        root
    end
    

    The above code got memory limit exceed. No idea.


  • 0
    L

    My code got memory limit exceed too.
    Don't know what to do...


Log in to reply
 

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