keep moving

###### 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) {
return getArrayList(1, n);
}

public List<TreeNode> getArrayList(int left,int right){
List<TreeNode> result=new ArrayList<TreeNode>();
if(left>right)
return result;
if(left==right){
return result;
}
for(int i=left;i<=right;i++){
List<TreeNode> leftList=getArrayList(left, i-1);
List<TreeNode> rightList=getArrayList(i+1, right);
if(leftList.size()==0){
for(int j=0;j<rightList.size();j++){
TreeNode root=new TreeNode(i);
root.right=rightList.get(j);
}
}
if(rightList.size()==0){
for(int j=0;j<leftList.size();j++){
TreeNode root=new TreeNode(i);
root.left=leftList.get(j);
}
}
for(int j=0;j<leftList.size();j++){
for(int k=0;k<rightList.size();k++){
TreeNode root=new TreeNode(i);
root.left=leftList.get(j);
root.right=rightList.get(k);
}
}
}
return result;
}
}

#### 95 Unique Binary Search Trees II

2015-11-28 12:59:53

#### 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 | Java最短代码实现

2016-03-09 20:43:19

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

2014-01-13 05:20:18

#### 96 Unique Binary Search Trees

2015-11-25 21:04:50

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

2015-01-14 15:25:46

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

2014-07-24 10:27:20

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

2015-08-10 16:14:47

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

2014-12-23 20:09:28

## 不良信息举报

Unique Binary Search Trees II