-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path894.all-possible-full-binary-trees.py
52 lines (42 loc) · 1.58 KB
/
894.all-possible-full-binary-trees.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
from hashlib import new
from tkinter.tix import Tree
from typing import List, Optional
from copy import copy
from treenode import TreeNode
class Solution:
def allPossibleFBT(self, n: int, mem={1: [TreeNode()]}) -> List[Optional[TreeNode]]:
if n % 2 == 0:
return []
else:
if n not in mem:
ans = []
for i in range(1, n, 2):
j = n - i - 1
for all_left in self.allPossibleFBT(i):
for all_right in self.allPossibleFBT(j):
t = TreeNode()
t.left = all_left
t.right = all_right
ans.append(t)
mem[n] = ans
return mem[n]
if __name__ == '__main__':
ss = Solution()
s3 = ss.allPossibleFBT(3)
e3 = [TreeNode(0, TreeNode(), TreeNode())]
assert e3 == s3
s7 = ss.allPossibleFBT(7)
e7 = [
TreeNode(0, TreeNode(0, TreeNode(), TreeNode()),
TreeNode(0, TreeNode(), TreeNode())),
TreeNode(0, TreeNode(0, TreeNode(0, TreeNode(),
TreeNode()), TreeNode()), TreeNode()),
TreeNode(0, TreeNode(0, TreeNode(), TreeNode(
0, TreeNode(), TreeNode())), TreeNode()),
TreeNode(0, TreeNode(), TreeNode(0, TreeNode(),
TreeNode(0, TreeNode(), TreeNode()))),
TreeNode(0, TreeNode(), TreeNode(0, TreeNode(
0, TreeNode(), TreeNode()), TreeNode()))
]
assert all(i7 in e7 for i7 in s7)
assert all(i7 in s7 for i7 in e7)