LeetCode之Unique Binry Search Trees
4月份很快就过半了,最近都在看WPF,有点落伍了...本来想写一点读书笔记的,还没想好要怎么写。所以为了能够达到每月一篇博客的目标,今天先说一个LeetCode上的面试题:Unique Binary Search Trees。
题目简单翻译过来就是说:
给定n个不同节点,问:可以构造出多少种异构的二叉搜索树。比方说n=3时有5种:
3 3 3 2 1
\ / / / \ \
2 2 1 1 3 2
/ / \ \
1 1 2 3
一般来说,遇到这种问题多半是找规律,总结出数学公式然后解决之。找规律前,首先要看我们有哪些可以利用的条件。
这里可用的条件很有限但也很明确:二叉搜索树。也就是其任意一部分二叉树都满足左子树节点都小于(或者大于)根节点;右子树节点都大于(或者小于)根节点。
我们已经有了n=3的结果,那么来看看n=4时的情况。我们知道,任何一个节点都可以成为一个根节点,那么:
- 如果以1为树根,那么剩下的三个节点都分布在该树的右子树上;
- 如果以2为树根,那么节点1必然是其左子树上的唯一节点,而3、4只可能在其优节点上;
- 如果以3为树根,那么节点2、3在其左子树上,而节点4是其右子树上的唯一节点;
- 如果以4为树根,那么甚于节点都在其左子树上;
这个简单的分析过程很快就将规律呈现了出来!!
对于n=4的情况,其异构结果就是各种独立情况下,左子树和右子树异构方式的排列数之和。用公式表示就是:f(4) = f(0) * f(3) + f(1) * f(2) + f(2) * f(1) + f(3) * f(0)。整一个典型的递归实现。唯一需要考虑的点就是你需要缓存一下中间数据,因为f(2)和f(3)分别被调用了2次。
class Solution {
public:
int numTrees(int n) {
static vector<int> cached(, );
if (n > cached.size())
{
cached.resize(n, );
}
if ( == n)
{
return ;
}
if (cached[n - ] != )
{
return cached[n - ];
}
else
{
for (int i = ; i < n; ++i)
{
cached[n - ] += numTrees(i) * numTrees(n - i - );
}
return cached[n - ];
}
}
};
顺带因为打算以后要有Python,所以附带上Python的代码。刚开始学着写Python,写着有点难过...(有没有更好的写法?)
class Solution:
# @return an integer
def numTrees(self, n):
if 0 == n:
return 1 cached = [1]
if n > len(cached):
result = 0;
for i in range(n):
index = n - i - 1 left_part_result = self.numTrees(i)
right_part_result = self.numTrees(index) result += left_part_result * right_part_result if index == len(cached):
cached[len(cached):] = [right_part_result] cached[len(cached):] = [result] return result
else:
return cached[n - 1];
这题目的背景是一个称之为Catalan Number的数。也可以参看百度百科。通过了解Catalan Number,我们会发现他的诸多应用。比方说凸多边形的最优三角划分就是这个问题的拓展。
LeetCode之Unique Binry Search Trees的更多相关文章
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 95. Unique Binary Search Trees II 唯一二叉搜索树 II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] 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(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
随机推荐
- “static”引发的一个错误
昨天晚上,舍友发来一个程序,先把代码贴上: #include<stdio.h>#define N 20short bufferA[N]={1,2,3,4,5,6,7,8,9,10,11, ...
- Makefile相关知识
1. Makefile的编写: 1>. makefile的命名 1. makefile 2. Makefile 2>. makefile中的规则 三部分: 目标(app):依赖(main. ...
- 转:在java中使用dom4j解析xml
JAVA 使用Dom4j 解析XML Java DOM4J Parser - Parse XML Document Dom4j下载及使用Dom4j读写XML简介 在java中使用dom4j解析xml ...
- ORACLE误删除ASM磁盘修复
在数据库运维中,总会遇到一些粗心大意的DBA,一不小心删除一些东西,这里举例讲解在误删除ASM磁盘之后,如果用KFED工具进行恢复: [grid@RAC1 ~]$ sqlplus / as sysas ...
- SQL DDL
Sql语言被分为四大类:数据查询语言(DQL),数据操纵语言(DML),数据定义语言(DDL),数据控制语言(DCL). 1. 数据查询语言(DQL) 数据查询语言基本结构由select子句,from ...
- Redis在JAVA中的运用(工具类)
最近项目需要用redis在中间做缓存所以写了一个工具类作为练习用 redis版本:redis_version:3.0.504 用到阿里的解析JSON的库:fastjson import org.apa ...
- zookeeper的zoo.cfg的配置
zookeeper的默认配置文件为zookeeper/conf/zoo_sample.cfg,需要将其修改为zoo.cfg.其中各配置项的含义,解释如下: tickTime:CS通信心跳时间 Zook ...
- 检查失败,<master>分支有过其他更新,请先在本地合并<master>分支的代码
- zencart産品描述加上錨文本
首先,函數會遍曆整段描述,假如一段描述裏面有Hermes wallets這個關鍵詞,那麽函數就會對這個關鍵詞加上鏈接,至于鏈接到哪裏,上面數組裏面有,隻要把數組裏面的内容替換你想要的就可以. 那麽在z ...
- composer 一些使用说明
1 使用订制的包 配置 "repositories": [ { "type": "path", "url": " ...