forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
43 lines (39 loc) · 1.1 KB
/
Solution.cs
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private List<TreeNode>[] f;
public IList<TreeNode> AllPossibleFBT(int n) {
f = new List<TreeNode>[n + 1];
return Dfs(n);
}
private IList<TreeNode> Dfs(int n) {
if (f[n] != null) {
return f[n];
}
if (n == 1) {
return new List<TreeNode> { new TreeNode() };
}
List<TreeNode> ans = new List<TreeNode>();
for (int i = 0; i < n - 1; ++i) {
int j = n - 1 - i;
foreach (var left in Dfs(i)) {
foreach (var right in Dfs(j)) {
ans.Add(new TreeNode(0, left, right));
}
}
}
f[n] = ans;
return ans;
}
}