-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path894 All Possible Full Binary Trees.py
57 lines (45 loc) · 1.6 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
53
54
55
56
57
#!/usr/bin/python3
"""
A full binary tree is a binary tree where each node has exactly 0 or 2 children.
Return a list of all possible full binary trees with N nodes. Each element of
the answer is the root node of one possible tree.
Each node of each tree in the answer must have node.val = 0.
You may return the final list of trees in any order.
Example 1:
Input: 7
Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],
[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
"""
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def __init__(self):
self.cache = {}
def allPossibleFBT(self, N: int) -> List[TreeNode]:
"""
recursive + memoization
"""
if N not in self.cache:
if N == 0:
ret = []
elif N == 1:
ret = [TreeNode(0)]
else:
ret = []
for i in range(N):
lefts = self.allPossibleFBT(i)
rights = self.allPossibleFBT(N-1-i)
# 0 or 2 child, cannot have only 1
if lefts and rights:
for left in lefts:
for right in rights:
node = TreeNode(0)
node.left = left
node.right = right
ret.append(node)
self.cache[N] = ret
return self.cache[N]