<p>Given an integer <code>n</code>, return <em>all the structurally unique <strong>BST&#39;</strong>s (binary search trees), which has exactly </em><code>n</code><em> nodes of unique values from</em> <code>1</code> <em>to</em> <code>n</code>. Return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/uniquebstn3.jpg" style="width: 600px; height: 148px;" />
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]
</pre>

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

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> [[1]]
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 8</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 {
    TreeNode* treeCopy(TreeNode* root){
        if(root == nullptr){
            return nullptr;
        }
        TreeNode* newRoot = new TreeNode(root->val);
        newRoot->left = treeCopy(root->left);
        newRoot->right = treeCopy(root->right);
        return newRoot;
    }
public:
    vector<TreeNode*> generateTrees(int n) {
        vector<TreeNode*> pre;
        if (n == 0)
        {
            return pre;
        }
        pre.push_back(nullptr);

        // Add one number at a time
        for (int i = 1; i <= n ; i++)
        {
            vector<TreeNode*> cur;

            // Traverse all previous solutions
            for (TreeNode* root: pre)
            {
                // Insert at the root
                TreeNode* insert = new TreeNode(i);
                insert->left = root;
                cur.push_back(insert);

                // Insert at right child, right child's right child, etc.
                for(int j = 0; j<=n; j++){
                    TreeNode* root_copy = treeCopy(root);
                    TreeNode* right = root_copy;
                    int k = 0;

                    // Traverse j times to find the right child
                    for(; k < j; k++)
                    {
                        if(right == nullptr) break;
                        right = right->right;
                    }

                    // If reached null, break early
                    if(right == nullptr) break;

                    // Save teh right subtree of the current right child as left child of insert node
                    TreeNode* rightTree = right->right;
                    insert = new TreeNode(i);
                    right->right = insert;
                    insert->left = rightTree;

                    //Add to the current result
                    cur.push_back(root_copy);
                }
            }
            
            pre = cur;
        }

        return pre;
    }
};
18000