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 ...
随机推荐
- Hibernate 配置详解(2)
6) hibernate.session_factory_name: 配置一个JNDI名称,通过Configuration对象创建的SessionFactory会绑定到JNDI下该名称中.一般名字格式 ...
- 【HDOJ】1987 Decoding
简单搜索. /* hdoj 1987 */ #include <iostream> #include <cstdio> #include <cstring> #in ...
- Unity 用C#脚本读取JSON文件数据
读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...
- Unity NGUI实现技能CD效果
unity版本:4.5.1 NGUI版本:3.6.5 脚本代码:C# 在游戏中经常要实现技能的CD效果,NGUI中已经实现了这个功能,即在button上创建一个半透明的Sprite实现这个功能. 首先 ...
- 牛逼的bootcss之buttons
css源码 /*! @license * * Buttons * Copyright 2012-2014 Alex Wolfe and Rob Levin * * Licensed under the ...
- 【转】EditText大小(长宽)的多种设置方式----不错
原文网址:http://orgcent.com/android-edittext-ems-layout-minwidth-maxheight/ EditText大小的设置有多种方式,要想对每种方式运用 ...
- Combination Sum III —— LeetCode
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Introduction to Glide, Image Loader Library for Android, recommended by Google
In the passed Google Developer Summit Thailand, Google introduced us an Image Loader Library for And ...
- NOI题库2454 雷涛的小猫
2454:雷涛的小猫 总时间限制: 20000ms 单个测试点时间限制: 10000ms 内存限制: 65536kB 描述 雷涛同学非常的有爱心,在他的宿舍里,养着一只因为受伤被救助的小猫(当然,这样 ...
- Postman 安装 & 资料
安装 下载地址: http://chromecj.com/web-development/2014-09/60/download.html 怎么在谷歌浏览器中安装.crx扩展名的离线Chrome插件? ...