Unique Binary Search Tree
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 分析:这道题的关键是找出规律,可以举几个例子帮助理解,化抽象为具体。实际上就是左子树书目*右子树的数目。此题用递归和循环都可以。用递归注意超时哦,此题我用的是循环。
class Solution {
public:
int numTrees(int n) {
int sum=;
if(n<=)
return n;
int* num=new int[n+];
num[]=;
num[]=;
for(int i=;i<=n;++i)
{
num[i]=;
for(int k=;k<i;++k)
{
int left=num[k];//保存左子树的数目
int right=num[i-k-];//保存右子树的数目
num[i]+=left*right;
}
}
sum=num[n];
delete[] num;
return sum;
}
};
python实现:
class Solution:
# @return an integer
def numTrees(self, n):
if n<=1:
return n
num=[]
for i in range(n+1):
num.append(0)
num[0]=1
num[1]=1
for j in range(2,n+1):
for k in range(j):
left=num[k]
right=num[j-k-1]
num[j]+=left*right
sum=num[n]
return sum
Unique Binary Search Tree的更多相关文章
- Unique Binary Search Tree II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析
本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...
- Unique Binary Search Tree - Leetcode
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Tree
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Leetcode 95. Unique Binary Search Tree II
由于BST的性质,所以右子树或者左子树中Node的值是连续的: 左子树 = [1, i -1], root = i, 右子树 = [i + 1, n].使用一个递归函数构造这个BST.其中返回值应该是 ...
- lintcode 中等题:unique Binary Search Tree 不同的二叉查找树
题目 不同的二叉查找树 给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种? 样例 给出n = 3,有5种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ \ 3 2 1 ...
- 【二叉查找树】01不同的二叉查找树的个数【Unique Binary Search Trees】
当数组为1,2,3,4,...,n时,基于以下原则构建的BST树具有唯一性: 以i为根节点的树,其左子树由[1,i-1]构成,其右子树由[i+1, n]构成. 我们假定f(i)为以[1,i]能产生的U ...
- 96. Unique Binary Search Trees (Tree; DP)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...
随机推荐
- 基于百度定位SDK的定位服务的实现
转载请标明出处:http://blog.csdn.net/android_ls/article/details/10179013 一.定位模块的需求:我们想知道使用我们应用的用户的大概位置,每隔五分钟 ...
- zabbix 添加jvm监控
1. zabbix 服务端安装,监控jmx 需要--enable-java zabbix 客户端不需要 --enable-java 2.zabbix_server端安装jdk 安装jdk [root@ ...
- Sumdiv(各种数学)
http://poj.org/problem?id=1845 题意:求A^B的所有约数的和再对9901取模: 做了这个学到了N多数学知识: 一:任意一个整数都可以唯一分解成素因子的乘积:A = p1^ ...
- UNITY3D ShadeSH9
UNITY3D ShadeSH9 属于Irradiance environment maps 方法,可以参考DX SDK PRTDemo,里面是几乎相同的实现,总之就是解光传输的积分方程 目前主流辐射 ...
- bzoj 1927 [Sdoi2010]星际竞速(最小费用最大流)
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1576 Solved: 954[Submit][Statu ...
- POJ3169 Layout(差分约束系统)
POJ3169 Layout 题意: n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛 ...
- Ubuntu server下安装JDK和Tomcat7
服务器是Ubuntu server 12.04 LTS 64bit 所有操作假设已经有root权限,若没有需要添加sudo. 一. 安装JDK 1.去Oracle官网下载jdk-6u45-linux- ...
- nodejs这个过程POST求
下面是一个web登陆模拟过程.当我们问一个链接,你得到一个表格,然后填写相应的表格值,然后提交登陆. var http = require('http'); var querystring = req ...
- [转] Nginx + CGI/FastCGI + C/Cpp
接着上篇<Nginx安装与使用>,本篇介绍CGI/FASTCGI的原理.及如何使用C/C++编写简单的CGI/FastCGI,最后将CGI/FASTCGI部署到nginx.内容大纲如下: ...
- objective c的注释规范
以前写android程序,每写一个函数或者类就添加对应的注释,后来领导让生成api文档,直接使用了eclipse的到处文档功能,强大又方便,这次换作ios开发,我想肯定也有对应的注释,于是上网查找了一 ...