作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/unique-binary-search-trees/description/

题目描述

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,问n个节点的二叉树有多少种?

解题方法

记忆化递归

思路:从1...n中找出一个i作为根节点,比i小的数1...i-1作为左子树,比i大的数i+1...n作为右子树,左子树的排列和右子树的排列的乘积是此时的数目。

因为直接递归会超时,所以加上了记忆化搜索的方法,这样就快的多了。

class Solution(object):
def __init__(self):
self.dp = dict() def numTrees(self, n):
"""
:type n: int
:rtype: int
"""
if n in self.dp:
return self.dp[n]
if n == 0 or n == 1:
return 1
ans = 0
for i in range(1, n + 1):
ans += self.numTrees(i - 1) * self.numTrees(n - i)
self.dp[n] = ans
return ans

使用C++代码和上面类似,同样使用记忆化搜索能够完成。只不过,这里需要注意的一点是,左边的孩子数目是i的时候,右边的孩子数目因该是n - 1 - i,因为要去掉根节点。

代码如下:

class Solution {
public:
int numTrees(int n) {
if (n == 0) return 1;
if (m_.count(n)) return m_[n];
int res = 0;
for (int i = 0; i < n; i++) {
int left = numTrees(i);
int right = numTrees(n - 1 - i);
res += left * right;
}
return m_[n] = res;
}
private:
unordered_map<int, int> m_;
};

动态规划

同样是上面的思路,如果使用动态规划去做,可以设dp[i]是i个节点的二叉树有多少种组合。那么,很明显和上面解法一样的,dp[i]等于左子树有0个节点,左子树有1个节点,左子树有2个节点……等等情况下的和。对于左右子树的组合方式是独立事件,所以总的组合数是左右子树相乘的关系。

完整的推导在下面,参照了:http://blog.csdn.net/u012501459/article/details/46622501

给定一个数n,求1到n这些数可以构成多少棵二叉树。
给定一个序列1.....n,为了构造所有二叉树,我们可以使用1......n中的每一个数i作为根节点,自然1......(i-1)必然位于树的左子树中,(i+1).....n位于树的右子树中。然后可以递归来构建左右子树,由于根节点是唯一的,所以可以保证构建的二叉树都是唯一的。 使用两个状态来记录: G(n):长度为n的序列的所有唯一的二叉树。 F(i,n),1<=i<=n:以i作为根节点的二叉树的数量。 G(n)就是我们要求解的答案,G(n)可以由F(i,n)计算而来。 G(n)=F(1,n)+F(2,n)+...+F(n,n) (1) G(0)=1,G(1)=1 对于给定的一个序列1.....n,我们取i作为它的根节点,那么以i作为根节点的二叉树的数量F(i)可以由下面的公式计算而来: F(i,n)=G(i-1)*G(n-i) 1<=i<=n (2) 综合公式(1)和公式(2),可以看出: G(n) = G(0) * G(n-1) + G(1) * G(n-2) + … + G(n-1) * G(0) 这就是上面这个问题的答案。

答案:

class Solution(object):
def numTrees(self, n):
"""
:type n: int
:rtype: int
"""
dp = [1, 1]
for i in xrange(2, n + 1):
count = 0
for j in xrange(i):
count += dp[j] * dp[i - j - 1]
dp.append(count)
return dp.pop()

上面的做法的C++代码如下:

class Solution {
public:
int numTrees(int n) {
// how many trees if the total tree has dp[i] nodes.
vector<int> dp(n + 1);
dp[0] = dp[1] = 1;
for (int i = 2; i < n + 1; i ++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[i - 1 - j];
}
}
return dp[n];
}
};

卡特兰数

卡塔兰数的一般项公式为

h(0)=1,h(1)=1,卡塔兰数数满足递归式:

h(n)= h(0)*h(n-1) + h(1)*h(n-2) + ... + h(n-1)h(0) (其中n>=2)这是n阶递归关系;

该递推关系的解为:

h(n)=C(2n,n)/(n+1)=P(2n,n)/(n+1)!=(2n)!/(n!*(n+1)!) (n=1,2,3,...)

代码如下:

class Solution {
public:
int numTrees(int n) {
// how many trees if the total tree has dp[i] nodes.
long long res = 1;
for (int i = n + 1; i <= 2 * n; i++) {
res = res * i / (i - n);
}
return res / (n + 1);
}
};

卡特兰数的前20项是固定的,也就可以直接返回对应的数字即可。

class Solution {
public:
int numTrees(int n) {
// how many trees if the total tree has dp[i] nodes.
vector<int> dp = {1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190};
return dp[n];
}
};

日期

2018 年 2 月 25 日
2018 年 12 月 31 日 —— 2018年最后一天!

【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)的更多相关文章

  1. [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  2. 52. leetcode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

  3. 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]* ...

  4. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  5. [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...

  6. Java [Leetcode 96]Unique Binary Search Trees

    题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...

  7. 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 ...

  8. [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数

    Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Input: ...

  9. [leetcode] 96 Unique Binary Search Trees (Medium)

    原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...

随机推荐

  1. Python基础之流程控制if判断

    目录 1. 语法 1.1 if语句 1.2 if...else 1.3 if...elif...else 2. if的嵌套 3. if...else语句的练习 1. 语法 1.1 if语句 最简单的i ...

  2. C4.5决策树-为什么可以选用信息增益来选特征

    要理解信息增益,首先要明白熵是什么,开始很不理解熵,其实本质来看熵是一个度量值,这个值的大小能够很好的解释一些问题. 从二分类问题来看,可以看到,信息熵越是小的,说明分类越是偏斜(明确),可以理解为信 ...

  3. Redis | 第8章 发布订阅与事务《Redis设计与实现》

    目录 前言 1. 发布订阅 1.1 频道的订阅与退订 1.2 模式的订阅与退订 1.3 发送消息 1.4 查看订阅消息 2. 事务 2.1 事务的实现 2.2 WATCH 命令的实现 2.3 事务的 ...

  4. 学习java的第二十一天

    一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...

  5. 《手把手教你》系列技巧篇(四十八)-java+ selenium自动化测试-判断元素是否可操作(详解教程)

    1.简介 webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelec ...

  6. acquire

    An acquired taste is an appreciation for something unlikely to be enjoyed by a person who has not ha ...

  7. Java实现读取文件

    目录 Java实现读取文件 1.按字节读取文件内容 使用场景 2.按字符读取文件内容 使用场景 3.按行读取文件内容 使用场景 4.随机读取文件内容 使用场景 Java实现读取文件 1.按字节读取文件 ...

  8. 最长公共子序列问题(LCS) 洛谷 P1439

    题目:P1439 [模板]最长公共子序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 关于LCS问题,可以通过离散化转换为LIS问题,于是就可以使用STL二分的方法O(nlogn ...

  9. vim一键整理代码命令

    vim下写代码超实用代码格式整理命令,仅需四步 ①先使用 gg 命令使光标回到第一行 ②shift+v 进入可视模式 ③shift+g 全选 ④按下  =  即可 混乱的代码格式 四步整理以后 工整又 ...

  10. Output of C++ Program | Set 4

    Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 1 #include<iostream ...