###### 【Leetcode】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 List<TreeNode> generateTrees(int n) {
if (n == 0)
return null;
return dsp(1, n);
}

public List<TreeNode> dsp(int start, int end) {
List<TreeNode> res = new ArrayList<TreeNode>();
if (start > end) {
return res;
}

for (int i = start; i <= end; i++) {
List<TreeNode> left = dsp(start, i - 1);
List<TreeNode> right = dsp(i + 1, end);
for (TreeNode l : left) {
for (TreeNode r : right) {
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
}
}
}
return res;
}


#### Unique Binary Search Trees -- LeetCode

2014-04-30 06:16:29

#### LeetCode | Unique Binary Search Trees II

2013-12-23 14:58:40

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

2014-01-13 05:20:18

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

2016-03-09 20:43:19

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

2014-07-24 10:27:20

#### [C++]LeetCode: 53 Unique Binary Search Trees

2014-12-23 20:09:28

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

2016-08-15 15:52:30

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

2015-08-10 16:14:47

#### leetCode 95.Unique Binary Search Trees II （唯一二叉搜索树） 解题思路和方法

2015-07-28 09:31:56

#### 95 Unique Binary Search Trees II

2015-11-28 12:59:53

## 不良信息举报

【Leetcode】Unique Binary Search Trees II