304 North Cardinal St.
Dorchester Center, MA 02124

# Unique Binary Search Trees II LeetCode Solution

## Problem – Unique Binary Search Trees II LeetCode Solution

Given an integer `n`, return all the structurally unique BST’s (binary search trees), which has exactly `n` nodes of unique values from `1` to `n`. Return the answer in any order.

Example 1:

``````Input: n = 3
Output: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]``````

Example 2:

``````Input: n = 1
Output: []``````

Constraints:

• `1 <= n <= 8`

### Unique Binary Search Trees II LeetCode Solution in Java

``````public static List<TreeNode> generateTrees(int n) {
List<TreeNode>[] result = new List[n + 1];
result = new ArrayList<TreeNode>();
if (n == 0) {
return result;
}

for (int len = 1; len <= n; len++) {
result[len] = new ArrayList<TreeNode>();
for (int j = 0; j < len; j++) {
for (TreeNode nodeL : result[j]) {
for (TreeNode nodeR : result[len - j - 1]) {
TreeNode node = new TreeNode(j + 1);
node.left = nodeL;
node.right = clone(nodeR, j + 1);
}
}
}
}
return result[n];
}

private static TreeNode clone(TreeNode n, int offset) {
if (n == null) {
return null;
}
TreeNode node = new TreeNode(n.val + offset);
node.left = clone(n.left, offset);
node.right = clone(n.right, offset);
return node;
}
``````

### Unique Binary Search Trees II LeetCode Solution in Python

``````def generateTrees(self, n):
def node(val, left, right):
node = TreeNode(val)
node.left = left
node.right = right
return node
def trees(first, last):
return [node(root, left, right)
for root in range(first, last+1)
for left in trees(first, root-1)
for right in trees(root+1, last)] or [None]
return trees(1, n)
``````

### Unique Binary Search Trees II LeetCode Solution in C++

``````class Solution {
public:
TreeNode* clone(TreeNode* root){
if(root == nullptr)
return nullptr;
TreeNode* newroot = new TreeNode(root->val);
newroot->left = clone(root->left);
newroot->right = clone(root->right);
return newroot;
}
vector<TreeNode *> generateTrees(int n) {
vector<TreeNode *> res(1,nullptr);
for(int i = 1; i <= n; i++){
vector<TreeNode *> tmp;
for(int j = 0; j<res.size();j++){
TreeNode* oldroot = res[j];
TreeNode* root = new TreeNode(i);
TreeNode* target = clone(oldroot);
root->left = target;
tmp.push_back(root);

if(oldroot!=nullptr){
TreeNode* tmpold = oldroot;
while(tmpold->right!=nullptr){
TreeNode* nonroot = new TreeNode(i);
TreeNode *tright = tmpold->right;
tmpold->right = nonroot;
nonroot->left = tright;
TreeNode *target = clone(oldroot);
tmp.push_back(target);
tmpold->right = tright;
tmpold = tmpold->right;
}
tmpold->right = new TreeNode(i);
TreeNode *target = clone(oldroot);
tmp.push_back(target);
tmpold->right = nullptr;
}
}
res=tmp;
}
return res;
}
};
``````
##### Unique Binary Search Trees II LeetCode Solution Review:

In our experience, we suggest you solve this Unique Binary Search Trees II LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Unique Binary Search Trees II LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Unique Binary Search Trees II LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions