leetcode — unique-binary-search-trees
/**
* 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的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 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 ...
- [leetcode]Unique Binary Search Trees @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Leetcode Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode: Unique Binary Search Trees [095]
[题目] Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
随机推荐
- [Tips]vim设置
临时设置 在vim中输入 :set nu! 若显示行号时,它的功能时取消行号:若不显示行号时,它的功能是显示行号. 固定设置 在~/.vimrc中进行设置. 添加注释: 双引号是注释 ” this i ...
- 基础java中的package的命名规则和import的使用
包的命名一般用公司域名但是注意域名后辍要放前面如下 package com.cnblogs.i.Cat//对应地址是com/cnblos/i/cat.class也就是Cat.class的地址 如果想将 ...
- vue 格式化银行卡(信用卡)每4位一个符号隔断
问题 在做银行卡输入框时有一个需求如题,这里举例用-隔断 调查 查看了很多大公司网站的银行卡输入,发现还有有很多缺陷的: 有的是在中间删除,光标会跳到最后: 有的是能删除掉中间隔断符的: 等等,逻辑感 ...
- vuex学习
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 简单的理解就是你在state中定义了一个数 ...
- (BUG记录)记一次与其他系统交互协作时造成的锁表问题
最近两日做公司电信某计费项目时,接收一个银行对账的任务,在完成对账后.电信和银行两方金额一致时需要进行充值.冲正操作保持金额一致.冲正服务是JAVA统一调用Tuxedo服务,这个服务已经是一个稳定可用 ...
- Dev_GridView:使用PopupContainerControl实现下拉树形列表
要使用 DevExpress 实现下拉列表树,需要使用三个控件结合才可以实现 PopupContainerEdit.PopupContainerControl.TreeList 设置控件 PopupC ...
- java发送短信验证码
业务: 手机端点击发送验证码,请求发送到java服务器端,由java调用第三方平台(我们使用的是榛子云短信http://smsow.zhenzikj.com)的短信接口,生成验证码并发送. SDK下载 ...
- 俄罗斯方块(三):"流动"的方块
问题的提出: 俄罗斯方块允许90度的坡,是不是有点不够科学#(滑稽) 想办法加一种会“滑坡”的方块 本文两大部分: 详细的描绘是怎样的“流动” 写代码,并整合进游戏 本文基于我写的 俄罗斯方块(一): ...
- 让MEF插上AOP的翅膀
什么是MEF Git:https://github.com/MicrosoftArchive/mef MEF也是一款ioc框架,貌似历史比较悠久了. 这里有一篇.net阵容里面主流ioc比较. htt ...
- CFUpdate高速模式下出现Error #2038提示的解决方案
使用CFUpdate上传文件,在IE模式下是正常的,切换到高速模式下出现提示Error #2038错误,文件无法上传. 向作者了解到需要设置challs_flash_update函数中的a.url为绝 ...