<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>

class Solution {
public:

    Solution()
    {
        storage.insert({ 0, {new TreeNode()} });
    }


    vector<TreeNode*> allPossibleFBT(int n) {
        if (n % 2 == 0)
        {
            return {};
        }
        else if (n == 1)
        {
            return { new TreeNode() };
        }
        else
        {

            std::list<TreeNode*> result = FBTMaker(n - 1);
            std::vector<TreeNode*> actualResult;
            actualResult.reserve(result.size());

            for (auto& x : result)
            {
                actualResult.push_back(x);
            }

            return actualResult;
        }

    }

    std::list<TreeNode*> FBTMaker(int n)
    {
        if (storage.contains(n))
        {
            return storage[n];
        }
        else
        {
            TreeNode* root = new TreeNode();
            put2Nodes(root);
            n -= 2;

            std::list<TreeNode*> output;

            int left = n;
            while (left >= 0)
            {
                std::list<TreeNode*> ll = FBTMaker(left);
                std::list<TreeNode*> rl = FBTMaker(n - left);

                // merge
                for (auto& l : ll)
                {
                    root->left = l;
                    for (auto& r : rl)
                    {
                        root->right = r;

                        TreeNode* temp = new TreeNode();
                        copyATree(root, temp);

                        output.push_back(temp);
                    }
                }

                left -= 2;
            }

            storage.insert({ n + 2, output });
            return output;
        }
    }
private:

    inline void put2Nodes(TreeNode* node)
    {
        node->left = new TreeNode();
        node->right = new TreeNode();
    }
    void copyATree(const TreeNode* source, TreeNode* destination)
    {
        if (!source)
            return;

        if (source->left)
        {
            destination->left = new TreeNode();
            copyATree(source->left, destination->left);
        }
        if (source->right)
        {
            destination->right = new TreeNode();
            copyATree(source->right, destination->right);
        }
    }

    std::map<int, std::list<TreeNode*>> storage;
};
41926