-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path95.py
30 lines (27 loc) · 1003 Bytes
/
95.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
from binarytree import build2
from itertools import product
class Solution():
def count_SBT(self, nums):
temp_res = {}
def count_BST_inners(start, end):
if start == end:
return [None]
if (start, end) in temp_res:
return temp_res[(start, end)]
res = []
for root_index in range(start, end):
left_count = count_BST_inners(start, root_index)
right_count = count_BST_inners(root_index+1, end)
for left,right in product(left_count, right_count):
root = build2([nums[root_index]])
root.left = left
root.right = right
res.append(root)
temp_res[(start, end)] = res
return res
res = count_BST_inners(0, len(nums))
return res
if __name__ == '__main__':
soul = Solution()
for node in soul.count_SBT(list(range(5))):
print(node)