【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 ...
随机推荐
- Centos7 搭建最新 Nexus3 Maven 私服
Maven 介绍 Apache Maven 是一个创新的软件项目管理和综合工具.Maven 提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文 ...
- Maven环境下搭建SSH框架之Spring整合Struts2
© 版权声明:本文为博主原创文章,转载请注明出处 1.搭建环境 Struts2:2.5.10 Spring:4.3.8.RELEASE 注意:其他版本在某些特性的使用上可能稍微存在差别 2.准备工作 ...
- 实现Nullable 可空类型
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace demo ...
- double check 解决单例模式的多线程并发问题
最近被多线程问题(multi-thread issue)弄昏了头.以前虽然也知道系统里要考虑多线程问题,也无数次见到double-check的代码,但是由于自己碰到这方面的问题基本上就是从其他地方 ...
- eclipse +cygwin+C++
用Android eclipse做C++开发,一开始提示no binary的错误,貌似是因为没有编译二进制出来,我本机装了cygwin, 在命令台输入gcc,无显示,说明我没有把cygwin/bin的 ...
- 使用Istio治理微服务入门
近两年微服务架构流行,主流互联网厂商内部都已经微服务化,初创企业虽然技术积淀不行,但也通过各种开源工具拥抱微服务.再加上容器技术赋能,Kubernetes又添了一把火,微服务架构已然成为当前软件架构设 ...
- 一次 read by other session 的处理过程
一个哥们给我打电话.他说系统中一直出现等待事件 read by other session .而且该等待都是同一个sql引起的.比較紧急,请我帮忙远程看看. 远程过去之后,用脚本把 等待事件给抓 ...
- centos7.0 安装redis 3.2.9
wget http://download.redis.io/releases/redis-3.2.9.tar.gz tar xzf redis-3.2.9.tar.gz cd redis-3.2.9 ...
- Java水印图片处理
今天需要用Java程序给图片加水印,于是在网上找到了一段代码,感觉很好,于是记录了下来,原来的网址给忘了: import java.awt.AlphaComposite; import java.aw ...
- 【BZOJ3661】Hungry Rabbit 贪心
[BZOJ3661]Hungry Rabbit Description 可怕的洪水在夏天不期而至,兔子王国遭遇了前所未有的饥荒,它们不得不去外面的森林里寻找食物.为了简化起见,我们假设兔子王国中有n只 ...