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 ...
随机推荐
- 【HDOJ】4932 Miaomiao's Geometry
递归检测.因为dis数组开的不够大,各种wa.写了个数据发生器,果断发现错误,改完就过了. #include <cstdio> #include <cstring> #incl ...
- BZOJ 1093 [ZJOI2007]最大半连通子图
1093: [ZJOI2007]最大半连通子图 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1986 Solved: 802[Submit][St ...
- SendMessage和PostMessage区别以及WPARAM 和 LPARAM区别
WPARAM 和 LPARAM wParam和lParam 这两个是Win16系统遗留下来的产物,在Win16API中WndProc有两个参数:一个是WORD类型的16位整型变量:另一个是LONG类型 ...
- vtk 中文显示
参考文章: http://www.vislab.cn/bbs/viewthread.php?tid=5079&page=1&extra=#pid19477 http://tieba.b ...
- 我的学习笔记之node----node.js+socket.io实时聊天(2)
废话不多说,直接贴代码吧.注释很详细了. 服务端代码: /** * Created by LZX on 2015/10/7. */(function () { var d = document, w ...
- WCF分布式事务
原文地址:http://developer.51cto.com/art/201002/185426.htm 我们作为一个开发人员,应该能够顺应技术的不断发展,不断的去掌握新技术.那么,对于WCF的掌握 ...
- ubuntu忘记密码
Do these two things just to make sure: mount -o remount,rw / This first part remounts the root parti ...
- mysql 循环插入100w
use md5db; DROP PROCEDURE if exists myFunction; delimiter $$ CREATE PROCEDURE myFunction() BEGIN DEC ...
- MongoDB 聚合
聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...
- android之interpolator的用法详解
Android:interpolator Interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerated(减速),repea ...