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

思路: f(n) = Σi=1n f(n-i)*f(i-1), 其中 f(0) = f(1) = 1; 利用动归记下之前的 f(2)~f(n-1)即可。
class Solution {
public:
int numTrees(int n) {
vector<int> f(n+1, 0);
f[0] = f[1] = 1;
for(int v = 2; v <= n; ++v)
for(int pos = 1; pos <= v; ++pos)
f[v] += f[pos-1] * f[v-pos];
return f[n];
}
};

Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.

For example, Given n = 3, your program should return all 5 unique BST's shown below.

1         3     3      2      1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3

思路:分别以 1~n 为根节点,左右子树根的集合数量相乘,递归,依次得出结果。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
vector<TreeNode *> generateTreesCore(int start, int end) {
vector<TreeNode *> vec;
if(start > end) { vec.push_back(NULL); return vec; }
for(int cur = start; cur <= end; ++cur) {
vector<TreeNode *> left = generateTreesCore(start, cur-1);
vector<TreeNode *> right = generateTreesCore(cur+1, end);
for(size_t i = 0; i < left.size(); ++i) {
for(size_t j = 0; j < right.size(); ++j) {
TreeNode *root = new TreeNode(cur);
root->left = left[i];
root->right = right[j];
vec.push_back(root);
}
}
}
return vec;
}
class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
return generateTreesCore(1, n);
}
};
           

41. Unique Binary Search Trees && Unique Binary Search Trees II的更多相关文章

  1. 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree

    1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...

  2. 04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  3. pat04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  4. [Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

    Binary search is an algorithm that accepts a sorted list and returns a search element from the list. ...

  5. 【Leetcode_easy】700. Search in a Binary Search Tree

    problem 700. Search in a Binary Search Tree 参考1. Leetcode_easy_700. Search in a Binary Search Tree; ...

  6. ERROR Shell: Failed to locate the winutils binary in the hadoop binary path

    文章发自:http://www.cnblogs.com/hark0623/p/4170172.html  转发请注明 14/12/17 19:18:53 ERROR Shell: Failed to ...

  7. WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path

    之前在mac上调试hadoop程序(mac之前配置过hadoop环境)一直都是正常的.因为工作需要,需要在windows上先调试该程序,然后再转到linux下.程序运行的过程中,报Failed to ...

  8. Hadoop:开发机运行spark程序,抛出异常:ERROR Shell: Failed to locate the winutils binary in the hadoop binary path

    问题: windows开发机运行spark程序,抛出异常:ERROR Shell: Failed to locate the winutils binary in the hadoop binary ...

  9. windows本地调试安装hadoop(idea) : ERROR util.Shell: Failed to locate the winutils binary in the hadoop binary path

    1,本地安装hadoop https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 下载hadoop对应版本 (我本意是想下载hadoop ...

  10. Windows7系统运行hadoop报Failed to locate the winutils binary in the hadoop binary path错误

    程序运行的过程中,报Failed to locate the winutils binary in the hadoop binary path  Java.io.IOException: Could ...

随机推荐

  1. Rhel6-torque作业调度系统配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.121 server21.example.com 计算节点 192.1 ...

  2. Asp.net MVC 视图(四)

    强类型辅助方法 模板辅助方法 Asp.net MVC中的模板辅助方法利用元数据和模板构建HTML,即:模板辅助方法可以通过使用数据注解,在运行时使用合适的任何“编辑器”来生成合适的HTML标记元数据包 ...

  3. 【LeetCode OJ】Distinct Subsequences

    Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...

  4. 表单验证 jQuery Validate

    http://www.runoob.com/jquery/jquery-plugin-validate.html http://www.cnblogs.com/linjiqin/p/3431835.h ...

  5. mac地址泛洪攻击的实验报告

    案例介绍: PC A 访问 本网络的一台FTPserver主机,中间人进行arp的投毒,获取PC-A和FTPserve之间的回话记录,截获用户名和密码. 实验拓扑:

  6. Python 决策树的构造

    上一节我们学习knn,kNN的最大缺点就是无法给出数据的内在含义,而使用决策树处理分类问题,优势就在于数据形式非常容易理解. 决策树的算法有很多,有CART.ID3和C4.5等,其中ID3和C4.5都 ...

  7. 转 UML类图几种关系的总结

    UML类图几种关系的总结   在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization),关联(Association),聚合(Aggregati ...

  8. cocos2d-x 3.2 listview scorllview 等容器在小米华为等部分手机显示泛白解决

    感觉记不住,代码贴上以免以后难找 在proj.android\src\org\cocos2dx\cpp\AppActivity.java 中的 public class AppActivity ext ...

  9. python ftplib.FTP 获取当前路径下所有目录

    FTP 模块里有一个dir函数,可以打印出当前路径下所有文件,但是这个函数没有返回值,只是打印出来. 还有一个nlst函数,可以返回一个文件名的列表,但是只有文件名,没有详细信息,无法判断是否是目录. ...

  10. python中的if __name__ == '__main__' what hell is it?

    python中的if __name__ == '__main__' what hell is it? python认为一切模块都可能被执行或者被import 如果一个模块是被import导入的,那么该 ...