<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:
    vector<TreeNode*> allPossibleFBT(int n) {
        if (n == 1) return { new TreeNode(0) };
        vector<TreeNode*> combos;
        unordered_map<int, vector<TreeNode*>> cache;
        combos = make_sub_trees(cache, n);

        return combos;
    }

    vector<TreeNode*> make_sub_trees(unordered_map<int, vector<TreeNode*>>& cache, int n) {
        if (n == 0) return {};
        if (n == 1) return {new TreeNode(0)};

        vector<TreeNode*> trees;
        int l = 1, r = n-2;
        while (l < n && n > 0) {
            vector<TreeNode*> left_sub_trees;
            vector<TreeNode*> right_sub_trees;
            
            if (cache.contains(l)) {
                left_sub_trees = cache[l];
            } else {
                left_sub_trees = make_sub_trees(cache, l);
                cache[l] = left_sub_trees;
            }
            if (cache.contains(r)) {
                right_sub_trees = cache[r];
            } else {
                right_sub_trees = make_sub_trees(cache, r);
                cache[r] = right_sub_trees;
            }

            for (TreeNode* left_tree : left_sub_trees) {
                for (TreeNode* right_tree : right_sub_trees) {
                    TreeNode* new_tree = new TreeNode(0);
                    TreeNode* temp_tree = new TreeNode(0);
                    temp_tree->left = left_tree;
                    temp_tree->right = right_tree;

                    deep_copy(temp_tree, new_tree);

                    trees.push_back(new_tree);
                }
            }

            l += 2;
            r -= 2;
        }

        return trees;
    }

    void deep_copy(TreeNode* orig, TreeNode* copy) {
        if (!orig) return;
        
        if (orig->left) {
            copy->left = new TreeNode(0);
            deep_copy(orig->left, copy->left);
        }
        if (orig->right) {
            copy->right = new TreeNode(0);
            deep_copy(orig->right, copy->right);
        }
    }
};
46218