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
注意:二分查找树的定义是,左子树节点均小于root,右子树节点均大于root!不要想当然地将某个点作为root时,认为其他所有节点都能全部放在left/right中,除非这个点是 min 或者 max 的。
 
分析:本题其实关键是递推过程的分析,n个点中每个点都可以作为root,当 i 作为root时,小于 i  的点都只能放在其左子树中,大于 i 的点只能放在右子树中,此时只需求出左、右子树各有多少种,二者相乘即为以 i 作为root时BST的总数。

 class Solution {
public:
int numTrees(int n) {
vector<int> v(n+,);
if(n<) return n;
v[]=;
for(int i=;i<=n;i++){
if(i<){
v[i]=i;
continue;
}
for(int j=;j<=i;j++){
v[i]+=v[j-]*v[i-j];
}
}
return v[n];
}
};

II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.

递归获取left——right的所有可能树,并存在vector中,以供上一层取值

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode *> generateTrees(int n) {
return sol(,n);
}
vector<TreeNode *> sol(int left, int right){
vector<TreeNode *> res; //保障res只会输出当前获取的树
if(left>right){
res.push_back(NULL); //否则返回后取值时会得到野指针
return res;
} for(int i=left;i<=right;i++){
vector<TreeNode *> leftlist=sol(left,i-);
vector<TreeNode *> rightlist=sol(i+,right);
for(int j=;j<leftlist.size();j++){
for(int k=;k<rightlist.size();k++){
TreeNode *root=new TreeNode(i);
root->left=leftlist[j];
root->right=rightlist[k];
res.push_back(root);
}
}
}
return res;
} };

Unique Binary Search Trees I&II——给定n有多少种BST可能、DP的更多相关文章

  1. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  2. Unique Binary Search Trees I & II

    Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...

  3. leetcode -day28 Unique Binary Search Trees I II

    1.  Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search t ...

  4. Unique Binary Search Trees I&&II(II思路很棒)——动态规划(II没理解)

      Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For exa ...

  5. [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  6. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  7. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  8. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

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

随机推荐

  1. java替换富文本标签等

    (转自:http://www.cnblogs.com/1246447850qqcom/p/5439366.html) package q;import java.util.regex.Matcher; ...

  2. input[type="radio"]自定义样式

    input为radio时,虽然会有默认选中的样式,但是并不符合大多数项目的需求,我们的目标是可以随心所欲自定义它的样式.怎么做呢?其实很简单,只要抓住3点.分别是1.label 2.隐藏自带样式 3. ...

  3. Transformer解析与tensorflow代码解读

    本文是针对谷歌Transformer模型的解读,根据我自己的理解顺序记录的. 另外,针对Kyubyong实现的tensorflow代码进行解读,代码地址https://github.com/Kyuby ...

  4. NodeJS学习(1)--- 安装配置介绍

    Node.js 安装配置 本章节我们将向大家介绍在window和Linux上安装Node.js的方法. 本安装教程以Node.js v6.10.1 LTS(长期支持版本)版本为例. Node.js安装 ...

  5. 转::iOS 仿淘宝,上拉进入详情页面

    今天做的主要是一个模仿淘宝,上拉进入商品详情的功能,主要是通过 tableView 与 webView 一起来实现的,当然也可根据自己的需要把 webView 替换成你想要的 // // ViewCo ...

  6. CodeForces 605 E. Intergalaxy Trips

    E. Intergalaxy Trips time limit per test:2 seconds memory limit per test:256 megabytes input:standar ...

  7. ZOJ1608 Two Circles and a Rectangle

    Time Limit: 2 Seconds      Memory Limit: 65536 KB Give you two circles and a rectangle, your task is ...

  8. no update

    cd /home/.gnupg/ mv gpg.conf gpgbake.conf pacman -S archlinux-keyring && pacman -Syu 如果还是不行, ...

  9. layui 的Tab选项卡

    http://www.layui.com/doc/element/tab.html <#--start--> <div class="layui-tab layui-tab ...

  10. Android,一条线串联实心圆布局

    最近遇到一个简单的布局,不是listview的形式.就只是单纯的下图这种: 此界面布局代码: <?xml version="1.0" encoding="utf-8 ...