-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertSortedArraytoBinarySearchTree(iterative).py
60 lines (49 loc) · 1.8 KB
/
ConvertSortedArraytoBinarySearchTree(iterative).py
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
'''
iterative(stack) - ac
use the mid of the array as root
left part for the l subtree
right part for the r subtree
in each iteration, construct a tuple, including
- subarray
- current node(will be the parent of next node)
- direction for the next node(left child or right child)
'''
def bst_iterative(arr):
if not arr:
return None
stack = []
mid = len(arr)//2
rootval = arr[mid]
lsub = arr[:mid]
rsub = arr[mid+1:]
treeroot = TreeNode(rootval)
stack.append((lsub,treeroot,'l'))
stack.append((rsub,treeroot,'r'))
while stack:
stacktop = stack.pop(-1)
curr = stacktop[0]
prev_node = stacktop[1]
direction = stacktop[2]
if not curr:
continue
mid = len(curr)//2
rootval = curr[mid]
lsub = curr[:mid]
rsub = curr[mid+1:]
curr_node = TreeNode(rootval)
if direction == 'r':
prev_node.right = curr_node
else:
prev_node.left = curr_node
stack.append((rsub,curr_node,'r'))
stack.append((lsub,curr_node,'l'))
return treeroot
return bst_iterative(nums)