<p>Given an integer <code>n</code>, return <em>a list of all possible <strong>full binary trees</strong> with</em> <code>n</code> <em>nodes</em>. Each node of each tree in the answer must have <code>Node.val == 0</code>.</p>

<p>Each element of the answer is the root node of one possible tree. You may return the final list of trees in <strong>any order</strong>.</p>

<p>A <strong>full binary tree</strong> is a binary tree where each node has exactly <code>0</code> or <code>2</code> children.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/22/fivetrees.png" style="width: 700px; height: 400px;" />
<pre>
<strong>Input:</strong> n = 7
<strong>Output:</strong> [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> [[0,0,0]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 20</code></li>
</ul>

/**
 * 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:
    TreeNode* clone(TreeNode* root){
        if(root==NULL)
        return NULL;
        TreeNode* r=new TreeNode(root->val);
        r->left=clone(root->left);
        r->right=clone(root->right);
        return r;
    }
    vector<TreeNode*> allPossibleFBT(int n) {
        vector<TreeNode*> ret;
        if(n==1){
            ret.push_back(new TreeNode(0));
            return ret;
        }
        if(n%2==0)
        return ret;

        vector<vector<TreeNode*>> cache;
        cache.push_back({});
        cache[0].push_back(new TreeNode(0));

        for(int root=1;root<n/2;root++){
            vector<TreeNode*> newroot;
          for(int left_sz=0;left_sz<root;left_sz++){
             for(TreeNode* left:cache[left_sz]){
                for(TreeNode* right:cache[root-left_sz-1]){
                    TreeNode* curr=new TreeNode(0);
                    curr->left=left;
                    curr->right=right;
                    newroot.push_back(curr);  
                }
             }
          }
          cache.push_back(newroot);
        }
        
        vector<TreeNode*> res;
        for(int root=0;root<n/2;root++){
           for(TreeNode* left:cache[root]){
              for(TreeNode* right:cache[n/2-root-1]){
                 TreeNode* newTree=new TreeNode(0);
                 newTree->left=clone(left);
                 newTree->right=clone(right);
                 res.push_back(newTree);
              }
           }
        }
        return res;
    }
};
38643