-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path109.py
More file actions
44 lines (37 loc) · 1.14 KB
/
109.py
File metadata and controls
44 lines (37 loc) · 1.14 KB
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
'''
109. Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order,
convert it to a height balanced BST.
[1, 2, 3, 4, 5, 6, 7]
'''
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def sortedListToBST(self, head):
"""
:type head: ListNode
:rtype: TreeNode
"""
return self.sortedListToBSTH(head, None)
def sortedListToBSTH(self, start, end):
if not start or start == end:
return None
slow = fast = start
while fast.next != end and fast.next.next != end:
slow = slow.next
fast = fast.next.next
if not fast:
print("None")
root = TreeNode(slow.val)
root.left = self.sortedListToBSTH(start, slow)
root.right = self.sortedListToBSTH(slow.next, end)
return root