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. UIwebView的html字符串高度计算

    ) { webView = [[UIWebView alloc]initWithFrame:CGRectMake(, , DEVW-, webviewH)]; webView.delegate = s ...

  2. 剑指offier第三题

    package 剑指office; /* * 第三题二维数组查找 * 在一个二维数组中,每一行都按照从左到右递增的顺序排序, * 每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维 ...

  3. xyiyy开始写博客了

    拖延症一直到现在才开始写博客... 希望写的博客对大家能有一些帮助,有不恰当或者不对的地方,还望大家指出. 以下为我的两个昵称:fraud xyiyy

  4. x^2+y^2=N的整数解?

    本文系转载:http://blog.sina.com.cn/s/blog_a661ecd50101cv41.html 我们先研究这个问题的一部分:哪些素数是两平方数之和?为什么我们先研究素数,有个很重 ...

  5. Centos安装php提示virtual memory exhausted: Cannot allocate memory

    由于内存不够,需要在php配置的时候./configure最后添加上 --disable-fileinfo >>./configure --prefix= ...........   -- ...

  6. 修改mysql编码为UTF-8

    mysql> show variables like '%character%'; +--------------------------+--------------------------- ...

  7. python成长之路第三篇(4)_作用域,递归,模块,内置模块(os,ConfigParser,hashlib),with文件操作

    打个广告欢迎加入linux,python资源分享群群号:478616847 目录: 1.作用域 2.递归 3.模块介绍 4.内置模块-OS 5.内置模块-ConfigParser 6.内置模块-has ...

  8. Effective Java2读书笔记-创建和销毁对象(二)

    第3条:用私有构造器或者枚举类型强化Singleton属性 这一条,总体来说,就是讲了一个小技巧,将构造器声明为private,可以实现单例.具体有以下几种实现的方式. ①最传统的单例实现模式,可能有 ...

  9. Django uplodify 多文件同时上传

    Js代码: //批量上传按钮 $('#fileupload').uploadify ({ 'swf' : '/CoveragePlaform/media/uploadify-3.2/uploadify ...

  10. Android studio修改debug.keystore

    在android studio项目中配置自定义的debug keystore 方法/步骤   在项目的build.gradle中添加如下内容: android {    signingConfigs ...