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 ...
随机推荐
- 转: RPC框架 远程对象服务引擎Hprose
http://www.cnblogs.com/chenxizhang/archive/2010/07/18/1780258.html
- json lib 2.4及其依赖包下载
下载文件地址:https://files.cnblogs.com/files/xiandedanteng/json-lib-2.4%26dependencies_jars.rar 它包括 common ...
- linux 之体验(JDK7+Tomcat7+MySQL5.5)部署环境
---------------------------------------------------------------------------------------------------- ...
- hdu1198Farm Irrigation(dfs找联通)
题目链接: 啊哈哈,选我选我 思路是:首先依据图像抽象出联通关系.. 首先确定每一种图形的联通关系.用01值表示不连通与不连通... 然后从第1个图形进行dfs搜索.假设碰到两快田地能够联通的话那么标 ...
- ID3算法Java实现
ID3算法java实现 1 ID3算法概述 1.1 信息熵 熵是无序性(或不确定性)的度量指标.假如事件A的全概率划分是(A1,A2,...,An),每部分发生的概率是(p1,p2,...,pn).那 ...
- 重读金典------高质量C编程指南(林锐)-------第一章 文件结构
第一章 文件结构 C/C++程序通常由两个文件组成,一个文件保存程序的声明,称为头文件,.h 文件.一个保存程序的实现,称为定义文件.c文件. 1.1 版权与版本的声明 版权和版本的声明 ...
- Oracle在plsql中修改数据
Oracle在plsql中想要修改数据,有两种方式: a.使用rowid+点击锁图标,语句为: select t.*,rowid from T_BIC_PLY_MAIN t; b.使用for up ...
- mysql 配置 安装和 root password 更改
第一步: 修改my.ini文件,替换为以下内容 (skip_grant_tables***重点) # For advice on how to change settings please see # ...
- 集群通信组件Tribes之怎样维护集群成员信息
一个集群包括若干成员,要对这些成员进行管理就必需要有一张包括全部成员的列表.当要对某个节点做操作时通过这个列表能够准确找到该节点的地址进而对该节点发送操作消息.怎样维护这张包括全部成员的列表是本节要讨 ...
- PHP通过prepare执行查询取得数据
可以用来防止sql注入 <?php $pdo=new PDO("mysql:host=localhost;dbname=itest", 'root',''); //先构建查询 ...