【LeetCode】96 - Unique Binary Search Trees
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) {
- if(n == )
- return ;
- else if(n == )
- return ;
- else
- {
- int count = ;
- for(int i = ; i <= (n-)/; i ++)
- {
- if(i < n--i)
- count += *numTrees(i)*numTrees(n--i);
- else
- count += numTrees(i)*numTrees(n--i);
- }
- return count;
- }
- }
- };
Solution 2: dynamic programming . Base case: n==0, n==1时,f(n)==1, f(n)=∑f(i)*f(n-i-1)。即以第i个为根节点,左右子树数目相乘
- class Solution {
- public:
- int numTrees(int n) {
- if(n<)return ;
- vector<int> v(n+, );
- v[]=;
- v[]=;
- for(int i=;i<n+;i++){
- for(int j=;j<i;j++){
- v[i]+=v[j]*v[i--j];
- }
- }
- return v[n];
- }
- };
【LeetCode】96 - Unique Binary Search Trees的更多相关文章
- 【LeetCode】96. Unique Binary Search Trees (2 solutions)
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- 【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化递归 动态规划 卡特兰数 日期 题目地址:ht ...
- 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)
[LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- 【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- 【一天一道LeetCode】#95. Unique Binary Search Trees II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [leetcode tree]96. 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? For examp ...
随机推荐
- jQuery-瀑布流-绝对定位布局(二)(延迟AJAX加载图片)
jQuery-瀑布流-绝对定位布局(二)(延迟AJAX加载图片) 瀑布流-绝对定位布局,与浮动布局的区别在于 1.布局不一样: 绝对定位:一个UL里面放置所有的绝对定位的LI: 浮动布局:多个(一 ...
- UX结合需求实例化进行设计开发
技 术 文 件 技术文件名称:实例化+UX需求分析实践:场景监控需求实例化 技术文件编号: 版 本:V1.0 共 32 页 (包括封面) 拟 制 廖开蒙.刀锋团队 审 核 ...
- SQL —— 一些需要注意的地方(持续更新)
TRUNCATE 只适用全表,没有 WHERE 语句 rownum < N 不能和 group by 一起使用 NULL 值通常会限制索引.在创建表时对某一列指定 NOT NULL 或 DEFA ...
- [原]poj2243-Knight Moves-水bfs
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...
- 安装 jdk、tomcat
jdk 下载地址:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java ...
- Introducing Microsoft Sync Framework: Sync Services for File Systems
https://msdn.microsoft.com/en-us/sync/bb887623 Introduction to Microsoft Sync Framework File Synchro ...
- 利用XPath读取Xml文件
之所以要引入XPath的概念,目的就是为了在匹配XML文档结构树时能够准确地找到某一个节点元素.可以把XPath比作文件管理路径:通过文件管理路 径,可以按照一定的规则查找到所需要的文件:同样,依据X ...
- 彻底搞清javascript中this, constructor, prototype
说起这三个属性,肯定有一些同学和我一样,初学js时非常困惑,头大,一脸的迷茫.今天就来给大家彻底解决这些担心受怕的问题. 先看this this定义: this就是函数赖以执行的对象. 分析这句话: ...
- PHPStorm/webstorm tips
phpstorm对于使用PHP开发web的人员来说,是一个非常不错的编辑开发IDE,以前用过sublime,但是相比于storm,sublime在浏览legacy代码,类代码编辑方面明显要逊色不少.同 ...
- HDU 3068 (Manacher) 最长回文
求一个字符串的最长子串,Manacher算法是一种O(n)的算法,很给力! s2[0] = '$',是避免在循环中对数组越界的检查. 老大的代码: http://www.cnblogs.com/Big ...