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. WIN7安装及配置JDK

    1:什么是JDK? JDK是Java Development Kit 的简称,即Java开发工具包.JDK是ORACLE公司针对Java开发者的产品,提供了Java的开发环境和运行环境. 更多信息参看 ...

  2. androidd 程序默认安装位置和数据存储位置(公用和私用)

    默认安装位置: android App 安装到外置SD卡中,缓解手机内置内存的压力: <manifest xmlns:android="http://schemas.android.c ...

  3. HRBUST 1867 差分+BIT

    我在群上看到的某道题,貌似用的是线段树,因为前几天遇到差分,再用BIT动态维护一下前缀和,感觉可做就A了. 加了个读优就Rank1啦! 某个不常见的题库,还是把题目拿下来把.. Description ...

  4. PHP正则表达式的快速学习方法

    1.入门简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例如,vi编辑器,Perl或PHP脚本语言,以及awk或 ...

  5. cocos2d/x 自带字体(label)

    CCLabelTTF* label1 = CCLabelTTF::labelWithString("1掼蛋as", "AppleGothic", 15); la ...

  6. 来自HeroKu的HTTP API 设计指南(中文版)

    原文转自:http://get.jobdeer.com/343.get 来自HeroKu的HTTP API 设计指南(中文版) 翻译 by @Easy 简介 本指南中文翻译者为 @Easy ,他是国内 ...

  7. sscanf,sscanf_s及其相关用法

    #include<stdio.h> 定义函数 int sscanf (const char *str,const char * format,........); 函数说明  sscanf ...

  8. 修改xubuntu14.04(同适用ubuntu)下Eclipse默认的黑色注释

    终端输入:sudo gedit /usr/share/themes/Ambiance/gtk-2.0/gtkrc 第一行将看到如下内容: gtk-color-scheme = "base_c ...

  9. C#里Attribute属性

    系统内置属性 系统内置的Attribute属性Obsolete,被个这属性标记的方法在别的地方被调用的时候会有警告提示; 这个属性还可以指定第二个布尔参数,设置编译时是否报错; 例: [Obsolet ...

  10. Flume+Kafka+Strom基于伪分布式环境的结合使用

    目录: 一.Flume.Kafka.Storm是什么,如何安装? 二.Flume.Kafka.Storm如何结合使用? 1) 原理是什么? 2) Flume和Kafka的整合  3) Kafka和St ...