-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path894.py
55 lines (53 loc) · 1.95 KB
/
894.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
__________________________________________________________________________________________________
sample 136 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def allPossibleFBT(self, N: int) -> List[TreeNode]:
def rec(N):
res = []
for i in range(1,N,2):
for left in self.memo[i]:
for right in self.memo[N-i-1]:
root = TreeNode(0)
root.left = left
root.right = right
res.append(root)
self.memo[N] = res
self.memo = {1:[TreeNode(0)]}
if N % 2 == 0:
return []
for i in range(3, N+1, 2):
rec(i)
return self.memo[N]
__________________________________________________________________________________________________
sample 16204 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def allPossibleFBT(self, N: int) -> List[TreeNode]:
if N % 2 == 0:
return [] # the number of node must be odd
cache = {}
for i in range(1,N+1,2): # only consider odd number
if i == 1: # initialize
cache[1] = [TreeNode(0)]
continue
cache[i] = []
for j in range(1,i,2): # j represent the number of node in left child
for lkid in cache[j]:
for rkid in cache[i-1-j]:
node = TreeNode(0)
node.left = lkid
node.right = rkid
cache[i].append(node)
return cache[N]
__________________________________________________________________________________________________