41. Unique Binary Search Trees && Unique Binary Search Trees II
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的更多相关文章
- 将百分制转换为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 ...
- 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 作者 ...
- 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 作者 ...
- [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. ...
- 【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; ...
- 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 ...
- WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path
之前在mac上调试hadoop程序(mac之前配置过hadoop环境)一直都是正常的.因为工作需要,需要在windows上先调试该程序,然后再转到linux下.程序运行的过程中,报Failed to ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- python 从文件导入分类
# -*- coding:utf-8 -*- """ 从文件导入分类 根据行首制表符或空格确定层级关系(4个空格等于一个制表符 同一行制表符和空格不能混用 ) 必须是 u ...
- Node.js 路由
我们要为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码. 因此,我们需要查看HTTP请求,从中提取出请求的URL以及GET/POST参数.这一功能应当属 ...
- enmo_day_09
1. 数据库 select name from v$database; : 数据库名称 select db_unique_name from v$database; : 数据库唯一名称 select ...
- js导入导出excel
导入: <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Unti ...
- css实现微信信息背景qq聊天气泡
用css实现一个椭圆形状的背景框很好实现 css: div{ width:200px; height:80px; background-color: #78DDF8; border-radius:10 ...
- C++ 基本知識回顧
---------------------------------------------------------------------------------------------------- ...
- [读书笔记] java类初始化
以下内容来自周志明的<深入理解java虚拟机>: 类初始化阶段是类加载过程的最后一步,前面的类加载过程中,除了在加载阶段用户应用程序可以通过自定义类加载器参与之外,其余动作完全由虚拟机主导 ...
- hdu3078 伪LCA……
题意:有 n 点的一颗树,每个节点有格子的权值,现在有两种操作,修改一个点的权值,或者求两点之间的路径上的第 k 大的权值. 其实看到这个题,就在 YY 各种做法,询问后得到貌似可能是关于主席树.树链 ...
- [转]js中的字符串函数集和代码片段
JS自带函数 concat将两个或多个字符的文本组合起来,返回一个新的字符串.var a = "hello";var b = ",world";var c = ...
- Network Address Translation(转载)
Network Address Translation 来源:http://alexanderlaw.blog.hexun.com/9791596_d.html 地址转换用来改变源/目的 ...