-
Notifications
You must be signed in to change notification settings - Fork 14
/
109-convert-sorted-list-to-binary-search-tree.py
48 lines (39 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
47
48
# 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]:
count = 0
cur = head
while cur:
count += 1
cur = cur.next
cur = head
def build_helper(left, right):
nonlocal cur
if left > right:
return None
mid = (left + right) // 2
left_subtree = build_helper(left, mid - 1)
node = TreeNode(cur.val)
cur = cur.next
node.left = left_subtree
right_subtree = build_helper(mid + 1, right)
node.right = right_subtree
return node
return build_helper(0, count - 1)
# time O(n)
# space O(logn), due to recursion stack
# using tree and divide and conquer and re-build BST (inorder approach)
'''
1. use divide and conquer, simulate inorder traversal
2. follow linked list ptr, so build left tree first then root then right tree
'''