-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert Sorted List to Binary Search Tree.py
More file actions
72 lines (63 loc) · 1.6 KB
/
Convert Sorted List to Binary Search Tree.py
File metadata and controls
72 lines (63 loc) · 1.6 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
# Definition for a binary tree node
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# @param head, a list node
# @return a tree node
def sortedListToBST(self, head):
current = head
array = []
while current:
array.append(current)
current = current.next
n = len(array)
return self.generateBST(n, array)
def generateBST(self, n, array):
if n == 0:
return None
if n == 1:
return TreeNode(array[0].val)
flag = n / 2
root = TreeNode(array[flag].val)
root.left = self.generateBST(n / 2, array[0:n / 2])
root.right = self.generateBST(len(array[n / 2 + 1:n]), array[n / 2 + 1:n])
return root
def preorderTraversal(root):
if root is None:
return []
stack = [root]
result = []
while len(stack) > 0:
current = stack.pop()
result.append(current.val)
if current.right is not None:
stack.append(current.right)
if current.left is not None:
stack.append(current.left)
return result
p1 = ListNode(1)
p2 = ListNode(2)
p3 = ListNode(3)
p4 = ListNode(4)
p5 = ListNode(5)
p6 = ListNode(6)
p7 = ListNode(7)
p8 = ListNode(8)
p1.next = p2
p2.next = p3
p3.next = p4
p4.next = p5
p5.next = p6
p6.next = p7
p7.next = p8
result = Solution()
ans = preorderTraversal(result.sortedListToBST(p1))
print ans