【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
【096-Unique Binary Search Trees(唯一二叉搜索树)】
【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】
原题
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
题目大意
给定一个n个结点的二叉搜索树,求一共同拥有多少个不同类型的二叉搜索树。
解题思路
递推公式
f(k)*f(n-1-k):f(k)表示根结点左子树有k个结点。其有的形状是f(k),f(n-1-k)表示右子树有n-1-k个结点
f(n) = 2*f(n-1) + f(1)*f(n-2) + f(2)*f(n-3) + f(3)*f(n-4) + … +f(n-2)*f(1)
代码实现
算法实现类
public class Solution {
public int numTrees(int n) {
if (n <= 0) {
return 0;
} else if (n == 1) {
return 1;
}
int[] result = new int[n + 1];
result[0] = 0;
result[1] = 1;
// 求f(2)...f(n)
for (int i = 2; i <= n; i++) {
// 求f(i)
result[i] = 2 * result[i - 1];
for (int j = 1; j <= i - 1 ; j++) {
result[i] += result[j]*result[i - 1 -j];
}
}
return result[n];
}
}
评測结果
点击图片。鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47333321】
【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】的更多相关文章
- [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] 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] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- 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 examp ...
- 096 Unique Binary Search Trees 不同的二叉查找树
给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种?例如,给出 n = 3,则有 5 种不同形态的二叉查找树: 1 3 3 2 1 ...
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (二叉搜索树最近的共同祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- python 数字计算模块 decimal(小数计算)
from decimal import * a = Decimal('0.1')+Decimal('0.1')+Decimal('0.1')+Decimal('0.3') float(a) >& ...
- Irrlicht 3D Engine 笔记系列 之 教程5- User Interface
作者:i_dovelemon 日期:2014 / 12 / 18 来源:CSDN 主题:GUI 引言 今天.博主学习了第五个教程. 这个教程解说了怎样使用Irrlicht内置的一个基础模块.GUI模块 ...
- 3.1 Broker Configs 官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 3.1 Broker Configs 3.1 broker配置 The essent ...
- mahout的特性(三)
mahout的特性 虽然在开源领域中相对较为年轻,但 Mahout 已经提供了大量功能,特别是在集群和CF 方面. Mahout 的主要特性包括: Taste CF.Taste 是 Sean Owen ...
- Office GVLK 密钥对照表(kms激活专用)
Office2016系列: Office Professional Plus 2016:XQNVK-8JYDB-WJ9W3-YJ8YR-WFG99 Office Standard 2016:JNRGM ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(0)-初步研究
2011年对着书本Android应用开发揭秘,写了2个月的HelloWorld. 现在想复习并深入,我没有耐心再去一点点地敲代码了. 4年前自己是个学生,实习,现在有工作,只能业余时间研究. ...
- 阅读笔记——Web应用程序
Web应用程序与DD文件 Web应用程序 web应用程序是一种可以通过Web访问的应用程序.Web应用程序最大的好处是永和很容易访问应用程序.用户只需要有浏览器即可,不需要安装其他任何软件.一个Web ...
- Windows下多个Mysql实例配置主从
序: 网上有很多类似的文章,也是各种百度出来的,但是对于多数刚开始接触MYSQL主从的小白来说,网上文章的代码里面很多技术点都没有理解,有跌打误撞碰上的,但多数都是这篇文章卡主了,换篇文章接着 ...
- Vijos——T 1164曹冲养猪
https://vijos.org/p/1164 描述 自从曹冲搞定了大象以后,曹操就开始捉摸让儿子干些事业,于是派他到中原养猪场养猪,可是曹冲满不高兴,于是在工作中马马虎虎,有一次曹操想知道母猪的数 ...
- JavaScript推断undefined的技巧
两种方法: 处理变量为undefined的情况: v = v||null; //假设v为undefined,则其值变为null 双感叹号:!!,把null/undifined/0转换为bolle ...