Untitled
unknown
plain_text
a year ago
466 B
11
Indexable
def sortedListToBST(self, head: Optional[ListNode]):
if not head:
return None
prev = node = mid = head
while node and node.next:
prev = mid
mid = mid.next
node = node.next.next
if head == mid:
return TreeNode(mid.val)
prev.next = None # cul left part of List
return TreeNode(mid.val, self.sortedListToBST(head), self.sortedListToBST(mid.next))Editor is loading...
Leave a Comment