-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConvertSortedListToBinarySearchTree_v1.py
More file actions
77 lines (62 loc) · 1.43 KB
/
ConvertSortedListToBinarySearchTree_v1.py
File metadata and controls
77 lines (62 loc) · 1.43 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
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
#!/usr/bin/env python
# encoding: utf-8
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# @param {ListNode} head
# @return {TreeNode}
p = None
def sortedListToBST(self, head):
count = 0
h = head
while h:
h = h.next
count += 1
self.p = head
return self.build_tree(count)
def build_tree(self, n):
if n == 0:
return
mid = n // 2
left = self.build_tree(mid)
cur = TreeNode(self.p.val)
cur.left = left
self.p = self.p.next
right = self.build_tree(n - mid - 1)
cur.right = right
return cur
def inorder(self, root):
if not root:
return
self.inorder(root.left)
print root.val
self.inorder(root.right)
if __name__ == '__main__':
s = Solution()
n1 = ListNode(1)
n2 = ListNode(2)
n3 = ListNode(3)
n4 = ListNode(4)
n5 = ListNode(5)
n6 = ListNode(6)
n7 = ListNode(7)
n8 = ListNode(8)
head = n1
n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5
n5.next = n6
n6.next = n7
n7.next = n8
h = s.sortedListToBST(head)
s.inorder(h)