###### Middle-题目64：95. Unique Binary Search Trees II

Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n.

For example,
Given n = 3, your program should return all 5 unique BST’s shown below.

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

public class Solution {
public List<TreeNode> generateTrees(int n) {
if(n==0) return new ArrayList<TreeNode>();
return generateTrees(1, n);
}

private List<TreeNode> generateTrees(int start, int end){
if(start>end) {
List<TreeNode> list = new ArrayList<TreeNode>();
return list;
}
if(start==end) {
List<TreeNode> list = new ArrayList<TreeNode>();
}
List<TreeNode> roots = new ArrayList<TreeNode>();
for(int i=start;i<=end;i++){
List<TreeNode> leftTrees = generateTrees(start, i-1);
List<TreeNode> rightTrees = generateTrees(i+1, end);
for(int j=0;j<leftTrees.size();j++){
for(int k=0;k<rightTrees.size();k++){
TreeNode root = new TreeNode(i);
root.left = leftTrees.get(j);
root.right = rightTrees.get(k);
}
}

}
return roots;
}
}

2ms,beats 88.62%,众数3ms,53.26%
cmershen的碎碎念：

#### 95 Unique Binary Search Trees II

2015-11-28 12:59:53

#### leetcode 95. Unique Binary Search Trees II | Java最短代码实现

2016-03-09 20:43:19

#### LeetCode(95)Unique Binary Search Trees II

2014-01-13 05:20:18

#### [leetcode-95]Unique Binary Search Trees II(c++)

2015-08-10 16:14:47

#### [C++]LeetCode: 92 Unique Binary Search Trees II

2015-01-14 15:25:46

#### leetcode-java-95. Unique Binary Search Trees II

2016-08-15 15:52:30

#### LeetCode | Unique Binary Search Trees II

2013-12-23 14:58:40

#### 96 Unique Binary Search Trees

2015-11-25 21:04:50

#### Unique Binary Search Trees -- LeetCode

2014-04-30 06:16:29

#### 【LeetCode】【Python题解】Unique Binary Search Trees

2014-07-24 10:27:20

## 不良信息举报

Middle-题目64：95. Unique Binary Search Trees II