【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:给定一个数n,找出1~n组成的二叉搜索树的个数!
可以参考【一天一道LeetCode】#95. Unique Binary Search Trees II
如果单单求个数的话,可以简化。
n=1时,num=1;n=2时,num=2;n=3时,num=5!
可以找出规律,num[i] = num[left]*num[right]!i从1取到n,就为num[n]的值。left和right为左右的节点个数。(节点个数小于等于1的时候记二叉搜索树个数为1)
以题目中的例子为例,取1为根节点,2,3组成的二叉搜索树个数为2(right),左边为1,所以1为根节点的二叉搜索树个数为2,依次可以算出,2为根节点时个数为1,3为根节点的个数为2,加起来为5!
class Solution {
public:
int numTrees(int n) {
vector<int> num(n+1,-1);//存放i个节点存放的二叉搜索树个数
int ret = calNumTrees(1,n,num);
return ret;
}
int calNumTrees(int start , int end , vector<int>& num)
{
if(num[end - start +1] != -1) return num[end - start +1];
if(start >= end) return 1;//当少于等于1个节点时,二叉搜索树个数记为1
int temp = 0;
for(int i = start ; i <= end ; i++)//依次以1到n为根节点
{
int left = calNumTrees(start,i-1,num);//左边二叉搜索树的个数
int right = calNumTrees(i+1,end,num);//右边二叉搜索树的个数
temp +=(left*right);
}
if(num[end - start +1] == -1) num[end-start+1] = temp;//num[i]存放1~i组成的个数
return temp;
}
};
【一天一道LeetCode】#96. 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 ...
- 52. leetcode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- [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 96 Unique Binary Search Trees ----- java
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Java [Leetcode 96]Unique Binary Search Trees
题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Input: ...
- [leetcode] 96 Unique Binary Search Trees (Medium)
原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...
- [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 ...
随机推荐
- Windows转Linux总结(附带常用Linux命令-LinuxMint)
这是我在Linux系统下写的第一篇博客,花了一周的时间从Windows系统转到Linux并且可以完成日常操作,能在Linux系统下完成开发,运用各种开发工具,写各种语言小程序和JavaEE. 经过这一 ...
- js改变dom对象样式
object.style.display = value; objcet对象必须是确定单个对象. 若以class名和标签名查找,需要指定对象集合中的第几个.
- 服务器&阵列卡LSI SAS223&组raid 10
组raid10 如配置: raid LSI SAS2236 双E5-2450L 96G 4*1TB 要求: 至少4块HDD 将接上Raid card的机器开机,根据提示按组合键进入Raid配置界面 ...
- OO第一阶段总结
OO第一阶段总结 一.各次作业程序结构 第一次作业 第一次作业由于初用JAVA,还没有深刻理解面向对象的编程方法,故在编程过程中只用了一个类,一个方法,即完成了相应的程序功能.这必然不是本课程的目的, ...
- 永远不要在循环之外调用wait方法
1. 前言 随着摩尔定律的失效,Amdahl定律成为了多核计算机性能发展的指导.对于现在的java程序员们来说,并发编程越来越重要和习以为常.很惭愧和恐慌的是我对java的并发编程一直是只知道概念,入 ...
- 网卡配置和DNS配置,手动挂在nas存储的共享目录,网络相关其它操作命令,修改防火墙中的端口配置,resolv.conf配置详细介绍和网卡信息配置详细介绍
1. 网卡配置和DNS配置 若想服务器能够发邮件,需要让部署的服务器能够访问到外网环境.若部署的服务器访问不到外网,通过ping www.baidu.com的方式执行的时候,会出现以下问题: &q ...
- Oracle11g R2创建PASSWORD_VERIFY_FUNCTION对应密码复杂度验证函数步骤
Oracle11g R2创建PASSWORD_VERIFY_FUNCTION对应密码复杂度验证函数步骤 运行测试环境:数据库服务器Oracle Linux 5.8 + Oracle 11g R2数据库 ...
- named let 递归和闭包的利器
named let和递归,闭包联系十分密切.而且还是提高性能的重要手段.先来看一个make-list函数的模拟,最原始的写法大概是: (define (imake-list n member) ( n ...
- Python 通过继承实现标准对象的子类
idict是dict的子类,它的键值和属性是同步的,并且有强大的默认值机制. 例如,假设x是idict的一个实例,且x['a']['b']=12,则有x.a.b=12.反之亦然; 假设'c'不在x的键 ...
- ELK平台的搭建
ELK是指Elasticsearch + Logstash + Kibaba三个组件的组合.本文讲解一个基于日志文件的ELK平台的搭建过程,有关ELK的原理以及更多其他信息,会在接下来的文章中继续研究 ...