-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path109.convert-sorted-list-to-binary-search-tree.py
46 lines (38 loc) · 1.29 KB
/
109.convert-sorted-list-to-binary-search-tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#
# @lc app=leetcode id=109 lang=python3
#
# [109] Convert Sorted List to Binary Search Tree
#
# @lc code=start
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def sortedListToBST(self, head: Optional[ListNode]) -> Optional[TreeNode]:
# Time: O(n)
# Space: O(log n), because the depth of the sortedListToBST recursion take log n space
if not head:
return
if not head.next:
return TreeNode(val=head.val)
# This base case is important such that slow won't overshoot
# If we start both slow and fast pointer at head it would not work
slow = head
fast = head.next.next
while fast and fast.next:
slow, fast = slow.next, fast.next.next
mid = slow.next
slow.next = None # Cut the linked list into two pieces
node = TreeNode(val=mid.val)
node.left = self.sortedListToBST(head)
node.right = self.sortedListToBST(mid.next)
return node
# @lc code=end