# Unique Binary Search Trees II

#1

Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.

Example:

```Input: 3
Output:
[
[1,null,3,2],
[3,2,null,1],
[3,1,null,null,2],
[2,1,3],
[1,null,2,null,3]
]
Explanation:
The above output corresponds to the 5 unique BST's shown below:

1         3     3      2      1
\       /     /      / \      \
3     2     1      1   3      2
/     /       \                 \
2     1         2                 3
```

#2

The below algorithm uses recursion to generate all the possible left sub trees and right sub trees at every `i` from `1 to n`.

``````/**
* Definition for a binary tree node.
* type TreeNode struct {
*     Val int
*     Left *TreeNode
*     Right *TreeNode
* }
*/
func generateTrees(n int) []*TreeNode {
if n==0{
return []*TreeNode{}
}
return genTreeList(1, n)
}

func genTreeList(start, end int) []*TreeNode{
fmt.Printf("start %d , end %d \n", start, end)
list := make([]*TreeNode, 0)
if start>end {
list = append(list, nil)
}

for i:=start;i<=end;i++{
leftList := genTreeList(start, i-1)
rightList := genTreeList(i+1, end)
for _, left := range leftList{
for _, right:= range rightList{
root := &TreeNode{Val : i}
root.Left = left
root.Right = right
list = append(list, root)
}
}
}

return list
}

``````