-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0894.cpp
More file actions
33 lines (28 loc) · 1.05 KB
/
Copy path0894.cpp
File metadata and controls
33 lines (28 loc) · 1.05 KB
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
class Solution {
TreeNode *duplicate(TreeNode *root) {
TreeNode *dup = new TreeNode(root->val);
if (root->left) dup->left = duplicate(root->left);
if (root->right) dup->right = duplicate(root->right);
return dup;
}
vector<TreeNode *> dp[20];
public:
vector<TreeNode *> allPossibleFBT(int n) {
if (n % 2 == 0) return {};
if (1 == n) return {new TreeNode(0)};
if (!dp[n].empty()) return dp[n];
vector<TreeNode *> ret;
for (int i = 2; i <= n; i += 2) {
auto left = allPossibleFBT(i - 1);
auto right = allPossibleFBT(n - i);
for (int l = 0; l < left.size(); l++) {
for (int r = 0; r < right.size(); r++) {
ret.push_back(new TreeNode(0));
ret.back()->left = (r == right.size() - 1) ? left[l] : duplicate(left[l]);
ret.back()->right = (l == left.size() - 1) ? right[r] : duplicate(right[r]);
}
}
}
return dp[n] = ret;
}
};