-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortedArrToBST.py
More file actions
88 lines (67 loc) · 2.29 KB
/
sortedArrToBST.py
File metadata and controls
88 lines (67 loc) · 2.29 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
78
79
80
81
82
83
84
85
86
87
88
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def sortedArrayToBST(self, nums):
import math
"""
:type nums: List[int]
:rtype: TreeNode
"""
def constructBST(arr):
if not arr or len(arr) == 0:
return None
# if startLen > endLen:
# return None
# midpoint = math.ceil((endLen + startLen) / 2)
midpoint = len(arr) // 2
if midpoint < len(arr):
tempNode = TreeNode(arr[midpoint])
tempNode.left = constructBST(arr[0:midpoint])
tempNode.right = constructBST(arr[midpoint + 1: len(arr)])
return tempNode
else:
return None
if not nums:
return None
else:
return constructBST(nums)
def bfs(self, root):
if root is None:
return []
queue = [root]
res = []
while queue:
level = []
for _ in range(len(queue)):
node = queue.pop(0)
level.append(node.val)
if node.left and node.right:
queue.append(node.left)
queue.append(node.right)
elif node.left:
queue.append(node.left)
elif node.right:
queue.append(node.right)
res.append(level)
return res
# def checkHeightBalanced(self, root):
# if root is None:
# return 0
# leftHeight = self.checkHeightBalanced(root.left)
# if leftHeight == -1:
# return -1
# rightHeight = self.checkHeightBalanced(root.right)
# if rightHeight == -1:
# return -1
# heightDiff = leftHeight - rightHeight
# if abs(heightDiff) > 1:
# return -1
# else:
# return max(leftHeight, rightHeight) + 1
tree = TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), None))
tempNode = Solution().sortedArrayToBST([1,2,3,4,5,6])
print(Solution().bfs(tempNode))