894. All Possible Full Binary Trees
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> allPossibleFBT(int N) {
if(N % 2 == 0) return {};
if(N == 1) return {new TreeNode(0)};
vector<TreeNode*> V;
N--;
for(int ii = 1; ii <= N; ii += 2) {
auto leftV = allPossibleFBT(ii), rightV = allPossibleFBT(N - ii);
for(auto a : leftV) {
for(auto b : rightV) {
TreeNode* root = new TreeNode(0);
root->left = a;
root->right = b;
V.push_back(root);
}
}
}
return V;
}
};