-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path894. All Possible Full Binary Trees.java
55 lines (48 loc) · 1.61 KB
/
894. All Possible Full Binary Trees.java
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
class Solution {
public List<TreeNode> allPossibleFBT(int n) {
List<TreeNode> res = new ArrayList<>();
/**
* Idea to solve the solution, **Recursion**:
* 1. For each node, we can have 0 or 2 children. to satisfy the full binary
* tree condition.
* 2. So, we can have 1 node and 0 children or 1 node and 2 children.
*
* the recurrence relation is: (we need to return the TreeNode)
*
* f(n) {
* for (int i = 1; i < n; i += 2) {
* List<TreeNode> left = f(i);
* List<TreeNode> right = f(n - i - 1);
* for (TreeNode l : left) {
* for (TreeNode r : right) {
* TreeNode root = new TreeNode(0);
* root.left = l;
* root.right = r;
* res.add(root);
* }
*/
// if n is even, then we can't have full binary tree
if (n % 2 == 0) {
return res;
}
// if n is 1, then we can have only 1 node
if (n == 1) {
res.add(new TreeNode(0));
return res;
}
// for each node, we can have 0 or 2 children
for (int i = 1; i < n; i += 2) {
List<TreeNode> left = allPossibleFBT(i);
List<TreeNode> right = allPossibleFBT(n - i - 1);
for (TreeNode l : left) {
for (TreeNode r : right) {
TreeNode root = new TreeNode(0); // default value
root.left = l;
root.right = r;
res.add(root);
}
}
}
return res;
}
}