【leetcode刷题笔记】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
解题: 有一个很简单的结论,就是节点数为n的二叉树共有h(n)种形态,其中h(n)是卡特兰数,h(n) = C(2*n,n)/(n+1);
所以只要按这个公式计算就是可以了。注意变量要取long long int型,否则会溢出。
代码:
class Solution {
public:
int numTrees(int n) {
long long int n_factorial = ;
long long int tn_fac = ;
for(int i = ;i <= n;i ++)
n_factorial *= i;
for(int i = n+;i <= *n;i ++)
tn_fac *= i;
return (tn_fac)/(n_factorial*(n+));
}
};
题外话:为什么节点数为n的二叉树有h(n)种形态呢?
当n=0时,h(0)=1;
当n=1时,h(1)=1;
当n>=2时,分别考虑树的左子树和右子树,它们的节点数分别可以取(0,n-1),(1,n-2),....,(n-2,1),(n-1,0),所以h(n)=h(0)*h(n-1)+h(1)*h(n-2)+...+h(n-1,0)h(0),这个递推式解得到的数就是卡特兰数。
【leetcode刷题笔记】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 exam ...
- LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II
1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...
- LeetCode(95) Unique Binary Search Trees II
题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【leetcode刷题笔记】Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode(96)Unique Binary Search Trees
题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- iOS项目工程添加.a文件遇到的Dsymutil Error
将.a文件加入工程,很多教程讲的都是: 右键选择Add->Existing Files…,选择.a文件和相应的.h头文件.或者将这两个文件拖入XCode工程目录结构中,在弹出的界面中勾选Copy ...
- myeclipse svn安装
安装subclipse, SVN 插件 1.从官网下载site-1.6.9.zip文件,网址是:subclipse.tigris.org, 2.从中解压出features与plugins文件夹,复制到 ...
- Error in as.POSIXlt.character(x, tz, ...) :
> sqlFetch(channel,"user")Error in as.POSIXlt.character(x, tz, ...) : character strin ...
- PHP性能之语言性能优化:魔术方法好不好?
魔术方法是什么鬼? 魔术方法,也叫魔鬼函数.只要学过PHP的都知道什么是魔术方法,魔术方法就是在某些条件下自动执行的函数. PHP的魔术方法主要有下面几个,其他的参考PHP官方手册 __constru ...
- centos 6.9使用Rsync+Inotify-tools实现数据实时同步
centos 6.9使用Rsync+Inotify-tools实现数据实时同步 说明: 操作系统:CentOS 6.9 源服务器:192.168.1.222 备份服务器:192.168.1.1.233 ...
- flex弹性盒模型
flex 意思是弹性布局,用来给盒模型提供最大的灵活度,指定容器中的项目为弹性布局,类似于float:left; 比float的好处是容器没有设置高度,会根据项目来自适应高度,我们都知道,设置floa ...
- android 学习mvc 和 mvp 和 mvvm参考项目
githup地址:https://github.com/ivacf/archi 阿尔奇 此存储库展示并比较可用于构建Android应用程序的不同架构模式.完全相同的示例应用程序使用以下方法构建三次: ...
- DB2恢复一例 SQL0928N
环境是AIX 6.1.DB2版本号9.7.0.7 首先查看db2主进程是否存在 ps -ef|grep db2sys 若不存在使用db2start打开数据库 备份介质为冷备数据源, cd 到介质所在文 ...
- 关于函数的return
function add(x, y) { var total = x + y; return total; } add(5,10); 关于函数的return 我一开始是认为没有什么用的 后来在项目中 ...
- iOS 启动页放大淡出效果
//屏幕宽度#define kWIDTH [UIScreen mainScreen].bounds.size.width//屏幕高度 #define kHEIGHT [UIScreen mainScr ...