-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConvertSortedListToBinarySearchTree_v0.py
More file actions
75 lines (60 loc) · 1.38 KB
/
ConvertSortedListToBinarySearchTree_v0.py
File metadata and controls
75 lines (60 loc) · 1.38 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
#!/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}
def sortedListToBST(self, head):
nodes = []
p = head
while p:
nodes.append(p)
p = p.next
return self._do(nodes)
def _do(self, nodes):
if not nodes:
return
lenn = len(nodes)
b = 0
e = lenn - 1
mid = (b+e) // 2
n = TreeNode(nodes[mid].val)
n.left = self._do(nodes[:mid])
n.right = self._do(nodes[mid+1:])
return n
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)