/**
* Source : https://oj.leetcode.com/problems/unique-binary-search-trees/
*
*
* Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
*
* For 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
*
*
*/
public class UniqueBinarySearchTree { /**
* 求出给定n的所有唯一的二叉搜索树
* 二叉搜索树:
* 如果左子树不为空,则左子树的节点值要小于根节点的值,如果右节点不为空,则右子树节点的值大于根节点的值
*
* 找规律
* 设n的唯一二叉搜索树个数为f(n)
* n = 0, f(0) = 1
* n = 1, f(1) = 1
* n = 2, f(2) = 2
* n = 3, f(3) = 5
* .....
*
* 讨论n = 3的情况
* 当根节点为1,左子树必须为空,则总数取决于右子树的个数,f(0)*f(n-1)=f(0)*f(2)=2
* 当根节点为2,左子树为1,右子树为3,在总数为f(1)*(n-2)=f(1)*(1)=1
* 当根节点为3,左子树为空,可能有f(0)*f(2),左子树为1,可能有f(1)*f(1),左子树为2,可能有f(2)*f(0)=2,总共有2+1+2=5
*
* 所以:
* f(0) = 1
* f(n) = f(0)*f(n-1) + f(1)f(n-2) + f(2)*f(n-3) + ... + f(n-3)f(2) + f(n-2)*f(1) + f(n-1)f(0);
*
* 注意:因为要计算f(n),所以数组长度应该为n+1,因为要保存0-n之间的结果
*
*
* @param n
* @return
*/
public int uniqueTreeCount (int n) {
if (n == 0) {
return 1;
}
int[] dp = new int[n+1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[i-j-1];
}
} return dp[n];
} public static void main(String[] args) {
UniqueBinarySearchTree uniqueBinarySearchTree = new UniqueBinarySearchTree();
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(0));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(1));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(2));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(3));
} }

leetcode — unique-binary-search-trees的更多相关文章

  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] Unique Binary Search Trees 独一无二的二叉搜索树

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

  3. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

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

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

  5. LeetCode - Unique Binary Search Trees II

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

  6. Leetcode: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 ...

  7. [leetcode]Unique Binary Search Trees @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...

  8. LEETCODE —— Unique Binary Search Trees [动态规划]

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

  9. Leetcode Unique Binary Search Trees

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

  10. LeetCode: Unique Binary Search Trees [095]

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

随机推荐

  1. WebBrowser加载一个URL被多次调用DocumentCompleted 的问题解决方案<转>

    关于DocumentCompleted事件,MSDN给出的解释是在文档加载完毕后执行,但是在我的程序中DocumentCompleted却被多次调用,查了一下资料,大概出现了以下几种情况. 1.Web ...

  2. 新建一个express项目的流程

    1.先创建一个文件夹,然后创建一个项目,默认有一个:package.json 文件 #初始化项目 npm init 2.初始化项目会出现一个默认的提醒 #这个实用程序将指导您创建一个包,json文件. ...

  3. 解决fastJson无序问题

    对外提供接口,第三方传过来的参数没问题.可是用fastJson 转换的出现 参数顺序不一致,导致 验签失败 解决fastJosn 转换无序问题 https://github.com/alibaba/f ...

  4. h5唤起APP并检查是否成功

    // 检查app是否打开 function checkOpen(cb) { const clickTime = +(new Date()); function check(elsTime) { if ...

  5. Redis安装及使用

    1.我们可以通过在官网下载tar.gz的安装包,或者通过wget的方式下载 进入要下载到的文件夹: wget http://download.redis.io/releases/redis-4.0.1 ...

  6. tp5生成6位不重复验证码

    /** * 获取激活码 */ public function getnum() { $time = Db::name('fangchan_jihuoma')->group('id desc')- ...

  7. android中Imageview的布局和使用

    布局: <ImageView android:id="@+id/imt_photo" android:layout_width="fill_parent" ...

  8. three.js的wave特效(ivew官网首页波浪特效实现)

    查看效果请访问:https://521lbx.github.io/Web3D/index.html公司的好几个vue项目都是用ivew作为UI框架,所以ivew官网时不时就得逛一圈.每一次进首页都会被 ...

  9. 产品经理教你如何构建电商电销 CRM 系统

    在电销或网销行业中老板们会经常问到,上个月渠道投放花了多少钱,来了多少量,转化率怎么样,获得了多少新线索,获客成本如何,销售额是多少? 劈天盖地的各种数据需求飞来,没有一个像样的系统该如何是好?这时候 ...

  10. 浅谈Java中的锁:Synchronized、重入锁、读写锁

    Java开发必须要掌握的知识点就包括如何使用锁在多线程的环境下控制对资源的访问限制 ◆ Synchronized ◆ 首先我们来看一段简单的代码: 12345678910111213141516171 ...