[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
此题是卡塔兰数的一个应用。注意是BST而不是普通的Binary Tree,所以要满足左比根小,右比根大。
- 1 n = 1
- 2 1 n = 2
- / \
- 1 2
- 1 3 3 2 1 n = 3
- \ / / / \ \
- 3 2 1 1 3 2
- / / \ \
- 2 1 2 3
Java: DP
- class Solution {
- public int numTrees(int n) {
- int[] count = new int[n + 1];
- count[0] = 1;
- count[1] = 1;
- for (int i = 2; i <= n; i++) {
- for (int j = 0; j <= i - 1; j++) {
- count[i] = count[i] + count[j] * count[i - j - 1];
- }
- }
- return count[n];
- }
- }
Python: Math
- class Solution(object):
- def numTrees(self, n):
- if n == 0:
- return 1
- def combination(n, k):
- count = 1
- # C(n, k) = (n) / 1 * (n - 1) / 2 ... * (n - k + 1) / k
- for i in xrange(1, k + 1):
- count = count * (n - i + 1) / i;
- return count
- return combination(2 * n, n) - combination(2 * n, n - 1)
Python: DP
- class Solution2:
- def numTrees(self, n):
- counts = [1, 1]
- for i in xrange(2, n + 1):
- count = 0
- for j in xrange(i):
- count += counts[j] * counts[i - j - 1]
- counts.append(count)
- return counts[-1]
C++:
- class Solution {
- public:
- int numTrees(int n) {
- vector<int> dp(n + 1, 0);
- dp[0] = 1;
- dp[1] = 1;
- for (int i = 2; i <= n; ++i) {
- for (int j = 0; j < i; ++j) {
- dp[i] += dp[j] * dp[i - j - 1];
- }
- }
- return dp[n];
- }
- };
类似题目:
[LeetCode] 96. Unique Binary Search Trees II 唯一二叉搜索树 II
All LeetCode Questions List 题目汇总
[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? Example ...
- [Leetcode] Unique binary search trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 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(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- 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]* ...
- 52. leetcode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- 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 ----- java
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [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初级算法--其他02:有效的括号
LeetCode初级算法--其他02:有效的括号 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- es6 字符串模板拼接和传统字符串拼接
字符串拼接是在日常开发中必不可少的一个环节. 注意:字符串可以用单引号'',或者""双引号,出于方便大家理解,文章以下内容统一使用单引号''! 如果只是一个字符串和一个变量拼接,使 ...
- sqlserver2005新特性介绍
1.更强的编程能力-CLR集成 增强了数据库的编程能力,将一些逻辑层(Bll)转移到数据库中,减少了网络中的数据流量,但是增加了服务器cpu的负荷,当我们需要操作大量的数据,但是产生很少的数据,把这种 ...
- IntelliJ IDEA 2019.2破解
IntelliJ IDEA 2019.2破解 我是参考这个激活的,使用的激活码的方式,需要在百度云盘下载压缩包 https://zhile.io/2018/08/25/jetbrains-licens ...
- Cocos2d-x学习小结 配置篇
Cocos2d-x学习小结 配置篇 学习工具:Cocos2d-x用户手册,<Cocos2d-x游戏开发之旅> 首先官网下载cocos2d-x源码,安装vs2019.如果没有安装python ...
- linux查看文件相关命令
通过命令+文件名查看内容.如下命令可以查看. 1,cat:由第一行开始显示文件内容:一次性显示文件所有内容 2,tac:从最后一行开始显示,可以看出tac与cat字母顺序相反:一次性显示文件所有内容, ...
- MongoDB 红宝书-MongoDB官网使用指南
本文转载自Mongodb中文社区:http://www.mongoing.com/archives/27359 无论你是MongoDB的使用者.爱好者.初学者还是路人甲,有一个学习与进修的资源宝藏是千 ...
- HBASE-LSM树(转载)
HBASE-LSM树 1.B+树 关于B树.B+树.B树的了解参考:* http://blog.csdn.net/v_july_v/article/details/6530142 优点: 走进搜索引擎 ...
- P2340 奶牛会展 DP 背包
P2340 奶牛会展 DP \(n\)头牛,每头牛有智商\(s[i]\)情商\(f[i]\),问如何从中选择几头牛使得智商情商之和最大 且 情商之和.智商之和非负 \(n\le 400,-10^3\l ...
- (12)Go面向对象
尽管Go中没有封装.继承.多态这些概念,但可以通过别的方式实现这个特性: *封装:通过方法实现 *继承:通过匿名字段实现 *多态:通过接口实现 package main import "fm ...