Python Solution with recursion

  • 0
    class Solution(object):
        def sortedListToBST(self, head):
            if not head:
                return None
            prev = None
            fast_runner = slow_runner = head
            while and
                prev = slow_runner
                fast_runner =
                slow_runner =
            root = TreeNode(slow_runner.val)
            if prev:
       = None
                head = None
            root.left = self.sortedListToBST(head)
            root.right = self.sortedListToBST(
            return root

Log in to reply

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