-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path894. All Possible Full Binary Trees.py
41 lines (33 loc) · 1.22 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
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self) -> str:
return str(self.val) + " " + str(self.left) + " " + str(self.right)
class Solution:
def allPossibleFBT(self, n: int):
dp = {0: [], 1: [TreeNode()]}
def backtracking(n):
if n in dp:
return dp[n]
result = []
for left in range(n - 1):
right = n - 1 - left
print("left", left, "right", right)
print("before rec", n)
left_nodes, rightnodes = backtracking(left), backtracking(right)
print("left_after rec", left_nodes, "right_after rec", rightnodes)
print("after rec", n)
for t1 in left_nodes:
for t2 in rightnodes:
result.append(TreeNode(0, t1, t2))
dp[n] = result
print("dp", dp)
print("dp", dp[n])
return result
return backtracking(n)
example = Solution()
n = 7
print(list(example.allPossibleFBT(n)))