<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) {}
 * };
 */
#include <unordered_map>

class Solution {
public:

    std::unordered_map<int, vector<TreeNode *>> dp_memoization;

    Solution() {
    }
    vector<TreeNode*> allPossibleFBT(int n) {

        vector<TreeNode*> ans;
        if (n == 1)
            return {new TreeNode(0)};

        if (n == 2)
            return {};

        if (n == 3){
            return {new TreeNode(0, new TreeNode(), new TreeNode())};
        }

        if (dp_memoization[n].size() > 0) // copy
            return duplicate_tree(dp_memoization[n]);

        for (int root = 2; root < n; root++){
            vector<TreeNode *> left = allPossibleFBT(root - 1);
            vector<TreeNode *> right = allPossibleFBT(n - root);

            if (left.size() > 0)
                dp_memoization[root - 1] = left;

            if (right.size() > 0)
                dp_memoization[n - root] = right;

            for (auto &l_node: left)
                for (auto &r_node: right) {
                    ans.push_back(new TreeNode(0, l_node, r_node));
                    //std::cout << "root:n " << root << ":" << n << std::endl;
                }
        }
        return ans;
    }

    std::vector<TreeNode *> duplicate_tree(std::vector<TreeNode *> &_vv){
        std::vector<TreeNode *> vv;
        for (auto &v: _vv)
            vv.push_back(v);
        
        return vv;
    }


#if 0
    vector<TreeNode*> allPossibleFBT(int n) {

        vector<TreeNode*> ans;
        if (n == 1)
            return {new TreeNode(0)};

        if (n == 2)
            return {};

        if (n == 3){
            return {new TreeNode(0, new TreeNode(), new TreeNode())};
        }

        for (int root = 2; root < n; root++){
            vector<TreeNode *> left = allPossibleFBT(root - 1);
            vector<TreeNode *> right = allPossibleFBT(n - root);
            for (auto &l_node: left)
                for (auto &r_node: right) {
                    ans.push_back(new TreeNode(0, l_node, r_node));
                    //std::cout << "root:n " << root << ":" << n << std::endl;
                }
        }

        return ans;
    }
#endif

};
39653