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. PHP 缓存扩展opcache

    opcache (全程 zend opcache): 从php5.5开始,默认提供的php脚本缓存扩展,编译php5.5时加上参数--enable-opcache就可以编译opcache了,只是要启用 ...

  2. Rhel6-pacemaker+drbd配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...

  3. Xcode5 运行程序 提示IOS 模拟器未能安装此应用程序

    更新了Xcode5,结果模拟器各种不配合,首先遇到的问题就是提示“IOS 模拟器未能安装此应用程序” 上网查了一下,网友给出的解决办法“删除~/Library/Application Support/ ...

  4. MVC 微信支付

    微信支付方式有好几种,俺研究了跟自己需要的两种,即:JS API网页支付和Native原生支付,这两个名词实在是有目的难懂.JS API网页支付:我的理解是在微信浏览器里面可以调用微信支付控件的支付方 ...

  5. github的注册过程

    带着疑问打开了github.这是一个神奇的网站,因为它到处都是英语,对于我这种英语盲这简直太痛苦了.借助了百度翻译,我还是马马虎虎的完成了github的制作. 首先在它的登录界面下面有一个sign u ...

  6. android sdk 更新用的HOSTS

    74.125.113.121 developer.android.com203.208.46.146 www.google.com 203.208.46.146 dl.google.com 203.2 ...

  7. 原生javascript加载运行

    原生javascript加载运行 (function(){ //TODO sometings }()); 在要运行相应代码的位置加入script标签,创建函数并自执行; 关于window.onload ...

  8. Binary Tree Inorder Traversal -- LeetCode 94

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  9. PCL中的类

    1. PCLBase pcl_base.h中定义了PCL中的基类PCLBase,PCL中的大部分算法都使用了其中的方法. PCLBase实现了点云数据及其索引的定义和访问. 两个主要的变量input_ ...

  10. Call to undefined function curl_init()

    运行PHP不支持curl_init()的解决方法: 1.修改php.ini,将;extension=php_curl.dll前面的分号去掉(同时检查扩展的引用路径是否正确)2.拷贝libeay32.d ...