Given n, how many structurally unique BSTs (binary search trees) that store values 1...n?

Example

Given n = 3, there are a total of 5 unique BST's.

1           3    3       2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
分析:

当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:以i为根节点的树,其左子树由[0, i-1]构成, 其右子树由[i+1, n]构成。那么,左子树由[0 , i -1]组成时,又有多少组合方式呢?这就可以递归了嘛。

 public class Solution {
public int numTrees(int n) {
int[] result = new int[n + ];
result[] = result[] = ; for (int i = ; i <= n; ++i) {
for (int j = ; j < i; ++j) { // j refers to the number of nodes in the left subtree
result[i] += result[j] * result[i - j - ];
}
}
return result[n];
}
}

Unique Binary Search Trees II

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

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
分析:
这题是要求把每一个树都找出来。从上一题我们知道,当root选取i时,它的左子树是节点从1到i -1的数组成的,右子树是i + 1到n的数组成的。那么我们创建一个函数,返回从 start 到end这两个值之间所有树的root.
 /**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @paramn n: An integer
* @return: A list of root
*/
public ArrayList<TreeNode> generateTrees(int n) {
return generate(, n);
} private ArrayList<TreeNode> generate(int start, int end){
ArrayList<TreeNode> treeList = new ArrayList<>(); if(start > end){
treeList.add(null);
return treeList;
} for(int i = start; i <= end; i++){
ArrayList<TreeNode> left = generate(start, i-);
ArrayList<TreeNode> right = generate(i+, end);
for(TreeNode l: left){
for(TreeNode r: right){
TreeNode root = new TreeNode(i);
root.left = l;
root.right = r;
treeList.add(root);
}
}
}
return treeList;
}
}

Unique Binary Search Trees I & II的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  2. leetcode -day28 Unique Binary Search Trees I II

    1.  Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search t ...

  3. Unique Binary Search Trees I&&II(II思路很棒)——动态规划(II没理解)

      Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For exa ...

  4. Unique Binary Search Trees I&II——给定n有多少种BST可能、DP

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  5. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  6. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  7. 41. Unique Binary Search Trees && Unique Binary Search Trees II

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  8. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  9. Unique Binary Search Trees,Unique Binary Search Trees II

    Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given  ...

随机推荐

  1. C基础--C语言的数组

    数组的定义: 一.正确写法: 1.int ages[5]--定义了一个5个长度的int类型的数组 2.int ages[]={1,2,3,4,5};--定义了一个5个长度的int类型的数组,并且初始化 ...

  2. hasSet,TreeSet,ArrayList,LinkedList,Vector,HashMap,HashTable,TreeMap利用Iterator进行输出

    基础类,没有重写hashCode()和equals()方法: package niukewang; import java.util.Objects; public class setClass { ...

  3. list 内部方法

    代码 #list内部方法 l=['a','9','c','a','3','7'] print(dir(l)) l.append('v') print(l)#append(self, p_object) ...

  4. codeforces 375D:Tree and Queries

    Description You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. ...

  5. linux在线学习

    https://www.shiyanlou.com/courses/running/291#note

  6. MVC 返回图片

    //调用 http://localhost:60663/home/GetCoder39Img?mycode=123443545 public void GetCoder39Img(string myc ...

  7. --hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包)

    解题思路: 多重背包:第 i 件物品有 j 个可用. 本题中 第 p[i] 类大米 有 c[i] 袋大米可买 ,故本题为多重背包. n(总钱数).m(种类) p[i] 单价 h[i] 重量 c[i] ...

  8. java可变参数例子:求学生成绩信息,不确定课程数

    可变参数特点: 1)...只能出现在参数列表的最后2)...位于变量类型和变量名之间3)调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法体中以数组的形式访问可变参数 //可变参数也可用 ...

  9. [LeetCode] next_permutation

    概念 全排列的生成算法有很多种,有递归遍例,也有循环移位法等等.C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用 ...

  10. 安装 RPM 包或者安装源码包

    安装 RPM 包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下. ...