Unique Binary Search Trees

Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium

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
class Solution {
public:
int numTrees(int n) {
vector<int> map;
map.push_back();
for (int i = ; i <= n; ++i) {
int t = ;
for (int j = ; j < i; ++j)
t += map[j] * map[i-j-];
map.push_back(t);
}
return map.back();
}
};

Unique Binary Search Trees II

Total Accepted: 45531 Total Submissions: 157430 Difficulty: Medium

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> generateTrees(int start,int end){
vector<TreeNode*> res;
if(end < start) {
res.push_back(NULL);
return res;
}
for(int rootValue = start; rootValue<=end; ++rootValue){
vector<TreeNode*> left = generateTrees(start,rootValue-);
vector<TreeNode*> right = generateTrees(rootValue+,end);
for(int i=;i<left.size();++i){
for(int j=;j<right.size();++j){
TreeNode *root = new TreeNode(rootValue);
root->left = left[i];
root->right = right[j];
res.push_back(root);
}
}
}
return res;
}
vector<TreeNode*> generateTrees(int n) {
return n== ? vector<TreeNode*>():generateTrees(,n);
}
};

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. 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 st ...

  7. Leetcode: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 st ...

  8. LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

    1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...

  9. Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树

    Unique Binary Search Trees:求生成二叉排序树的个数. Given n, how many structurally unique BST's (binary search t ...

随机推荐

  1. build opencv with python support

    cmake -DPYTHON_LIBRARY=/opt/anaconda/lib/libpython2.7.so -DPYTHON_INCLUDE_DIR=/opt/anaconda/include/ ...

  2. 配置Tomcat中的Context元素中的中文问题

    发布一个名叫helloapp的web应用,helloapp位于D:\我\helloapp.发布的方式是通过配置<CATALINA_HOME>/conf/Catalina/localhost ...

  3. Mapper映射语句高阶应用——ResultMap

    resultMap 元素是MyBatis 中最重要最强大的元素.它就是让你远离 90%的需要从结果 集中取出数据的 JDBC代码的那个东西, 而且在一些情形下允许你做一些 JDBC 不支持的事 情. ...

  4. js继承模式

    组合继承是js常用的继承模式,指的是将原型链和借用构造函数的技术结合在一起.其中的思想是使用原型链实现原型属性和方法的继承, 而通过借用构造函数实现对属性的继承. 例子: <script> ...

  5. python运维开发(十八)----Django(二)

    内容目录 路由系统 模版 Ajax model数据库操作,ORM 路由系统 django中的路由系统和其他语言的框架有所不同,在django中每一个请求的url都要有一条路由映射,这样才能将请求交给对 ...

  6. 基于cygwin构建u-boot(二)gcc的C语言标准版本号(-std=)

    接上文,修改Makefile和config.mk中,.depend相关文件格式后继续编译. 四.tools文件夹的文件编译错误 错误告警如下[多行信息,请使用右侧滚动条查看]: gcc -Wall - ...

  7. MySQL - 建库、建表、查询

    本章通过演示如何使用mysql客户程序创造和使用一个简单的数据库,提供一个MySQL的入门教程.mysql(有时称为“终端监视器”或只是“监视”)是一个交互式程序,允许你连接一个MySQL服务器,运行 ...

  8. Meteor Shower(POJ 3669)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12816   Accepted: 3451 De ...

  9. BZOJ NOI十连测 第一测 T2

    思路:看到这题,就感觉是一道很熟悉的题目: http://www.cnblogs.com/qzqzgfy/p/5535821.html 只不过这题的K最多可以到N,而且边权不再只是1,考试的时候yy了 ...

  10. jQuery开发经验实例笔记

    jQuery获取文件选择输入框的扩展名 var file=$("input[name='file']").val() var filename=file.replace(/.*(\ ...