Java for LeetCode 096 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.
解题思路:
二叉查找树,刚上来无从下手,但仔细想想就能发现规律:以i为根节点,i的左子树都是小于i的,共有numTrees(i-1)种情况,i的右子树都是大于i的,共有numTrees(n-i) 种情况,相乘即是以i为根节点的情况,然后依次循环即可,递归下即可解决。当然更高效的方式是采用DP来替代递归,JAVA实现如下:
public int numTrees(int n) {
if (n <= 0)
return 0;
int[] dp = new int[n + 1];
dp[0] = 1;
for (int i = 1; i < dp.length; i++)
for (int k = 0; k <= i - 1; k++)
dp[i] += dp[k] * dp[i - k - 1];
return dp[n];
}
Java for LeetCode 096 Unique Binary Search Trees的更多相关文章
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
随机推荐
- pair类型
pair是一个模板数据类型,其中包含两个数据值,两个数据值可以不同 如 pair<int,string>a(2,"fgh");则a是一个pair类型,它包括两个数据,第 ...
- EasyMvc入门教程-高级控件说明(16)信息框控件
网页开发的时候,会经常向用户显示一些信息,比如执行结果,注意事项等,下面展示一种最简单的信息提示: 代码如下: @Html.Q().Msg().Text("初始化信息").Auto ...
- struts2.16中文乱码问题解决
方法1.在struts.xml文件中添加<constant name="struts.i18n.encoding" value="GBK" /> 方 ...
- 2016.6.20 eclipse中的jsp文件的字体大小在哪里修改
刚打开eclipse的时候,觉得jsp文件的字体太小了.于是去修改字体,但是colors and fonts里的字体选项太多了,不知道哪一个是. 试了几个后发现,是structured text ed ...
- 更新到mysql 5.7后解决0000-00-00日期问题
更新到mysql 5.7后解决0000-00-00日期问题 学习了:http://www.07net01.com/2016/04/1479450.html mysql 5.7 默认开始用以下sql m ...
- Spring boot Security Disable security
When I use security.basic.enabled=false to disable security on a Spring Boot project that has the fo ...
- grep 精确匹配
使用grep实现精确过滤的五种方法 (1)当被过滤的内容占据一行时 [root@MySQL scripts]# cat oldboy.log 200 0200 2000 [root@My ...
- spring(16)------spring的数据源配置
在spring中,通过XML的形式实现数据源的注入有三种形式. 一.使用spring自带的DriverManagerDataSource 使用DriverManagerDataSource配置数据源与 ...
- Solr局部或指定字段更新之set用法
solr wiki文档也有 http://yonik.com/solr/atomic-updates/ java code public static void up ...
- NodeJS待重头收拾旧山河(重拾)
介绍 Node.js®是一个基于Chrome V8 JavaScript引擎构建的JavaScript运行时. Node.js使用事件驱动的非阻塞I / O模型,使其轻便且高效. Node.js的包生 ...