-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_possible_full_binary_trees.py
55 lines (38 loc) · 1.48 KB
/
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
"""
Given an integer n, return a list of all possible full binary trees with n nodes. Each node of each tree in the answer must have Node.val == 0.
Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.
A full binary tree is a binary tree where each node has exactly 0 or 2 children.
Example 1:
Input: n = 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]]
Example 2:
Input: n = 3
Output: [[0,0,0]]
Constraints:
1 <= n <= 20
"""
# 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
class Solution:
def allPossibleFBT(self, n: int) -> List[Optional[TreeNode]]:
if n % 2 == 0:
return []
def dfs(n):
if n == 0:
return []
if n == 1:
return [TreeNode()]
ans = []
for nodes_in_left_subtree in range(n-1):
nodes_in_right_subtree = n - 1 - nodes_in_left_subtree
left_tree = dfs(nodes_in_left_subtree)
right_tree = dfs(nodes_in_right_subtree)
for left in left_tree:
for right in right_tree:
ans.append(TreeNode(0, left, right))
return ans
return dfs(n)