-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAll Possible Full Binary Tree.cpp
46 lines (43 loc) · 1.31 KB
/
All Possible Full Binary Tree.cpp
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<TreeNode*>> dp;
vector<TreeNode*> solve(int n) {
if (n % 2 == 0)
return {}; // Return empty vector for even n
if (n == 1){
dp[1]={new TreeNode()};
return dp[1];
}
if (dp[n].size()>0)
return dp[n];
vector<TreeNode*> ans;
for (int i = 1; i <n; i += 2) {
vector<TreeNode*> Left = solve(i);
vector<TreeNode*> Right = solve(n - 1 - i);
for (TreeNode* l : Left) {
for (TreeNode* r : Right) {
TreeNode* root = new TreeNode();
root->left=l;
root->right=r;
ans.push_back(root);
}
}
}
return dp[n] = ans;
}
vector<TreeNode*> allPossibleFBT(int n) {
dp.assign(n + 1, {});
return solve(n);
}
};