-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertSortedListToBinarySearchTree.py
95 lines (61 loc) · 2.02 KB
/
ConvertSortedListToBinarySearchTree.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from typing import Optional
from linked_list.ListNode import ListNode
from trees.TreeNode import TreeNode
# O(n) time || O(log(n)) space
def sorted_list_to_bst(self, head: Optional[ListNode]) -> Optional[TreeNode]:
size = find_size(head)
def convert(low, high):
nonlocal head
if high < low:
return None
mid = low + (high - low) // 2
left = convert(low, mid - 1)
node = TreeNode(head.val)
node.left = left
head = head.next
node.right = convert(mid + 1, high)
return node
return convert(0, size - 1)
def find_size(head):
curr, size = head, 0
while curr:
curr, size = curr.next, size + 1
return size
# O(n) time || O(n) space
def sorted_list_to_bst_using_array(self, head: Optional[ListNode]) -> Optional[TreeNode]:
if not head:
return
if not head.next:
return TreeNode(head.val)
arr = linked_list_to_array(self, head)
def convert_arr_to_bst(low, high):
if high < low:
return None
mid = low + (high - low) // 2
node = TreeNode(arr[mid])
if low == high:
return node
node.left = convert_arr_to_bst(low, mid - 1)
node.right = convert_arr_to_bst(mid + 1, high)
return node
return convert_arr_to_bst(0, len(arr) - 1)
def linked_list_to_array(self, head: Optional[ListNode]) -> []:
arr = []
while head:
arr.append(head.val)
head = head.next
return arr
# O(max(n * log(n), n^2)) time || O((log(n), n)) space
def sorted_list_to_bst_rec(self, head: Optional[ListNode]) -> Optional[TreeNode]:
if not head:
return
if not head.next:
return TreeNode(head.val)
prev, slow, fast = None, head, head
while fast and fast.next:
prev, slow, fast = slow, slow.next, fast.next.next
prev.next = None
root = TreeNode(slow.val)
root.left = sorted_list_to_bst_rec(self, head)
root.right = sorted_list_to_bst_rec(self, slow.next)
return root