python solution using fast, slow pointers


  • 0
    M
        def sortedListToBST(self, head):
            if head is None:
                return None
            s, f, t = head, head, None
            while f and f.next:
                t = s
                s = s.next
                f = f.next.next
            if t:
                t.next = None
            else:
                head = None
            tree = TreeNode(s.val)
            
            tree.left = self.sortedListToBST(head)
            tree.right = self.sortedListToBST(s.next)
            
            return tree
    

Log in to reply
 

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