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

二叉排序树(二叉排序树(Binary Sort Tree)又称二叉查找树(Binary Search Tree),亦称二叉搜索树。)或者是一棵空树,或者是具有下列性质的二叉树

(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
(4)没有键值相等的节点。
对应leetcode中,n个数中中每个数都可以作为root,当 i 作为root时,小于 i  的点都只能放在其左子树中,大于 i 的点只能放在右子树中,此时只需求出左、右子树各有多少种,二者相乘即为以 i 作为root时BST的总数。
因为递归过程中存在大量的重复计算,从n一层层往下递归,故考虑类似于动态规划的思想,让底层的计算结果能够被重复利用,故用一个数组存储中间计算结果(即 1~n-1 对应的BST数目),这样只需双层循环即可,代码如下:

class Solution {
public:
int numTrees(int n) {
vector<int> num;
if (n<1)
return 0;
if(n==1)
return 1;
if(n==2)
return 2;
num.push_back(1);
for(int i=1;i<3;i++)
num.push_back(i);
for(int i=3;i<=n;i++)
{
num.push_back(0);
for(int j=0;j<i;j++)
num[i]+=num[j]*num[i-j-1];
}
return num[n];
}
};

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

  

对于II,网上没看到用动态规划的,都是暴力解法——枚举。用递归完成,还是得感慨一下,我的代码思维好乱啊,不像是个理科生啊!!!!!

/**
* 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 *> generateRes(int left,int right)
{
vector<TreeNode *> res;
if(left>right)
{
res.push_back(NULL);
return res;
}
for(int i=left;i<=right;i++)
{
vector<TreeNode *>leftpart=generateRes(left,i-1);
vector<TreeNode *>rightpart=generateRes(i+1,right);
for(int j=0;j<leftpart.size();j++)
for(int k=0;k<rightpart.size();k++)
{
TreeNode* node=new TreeNode(i);
node->left=leftpart[j];
node->right=rightpart[k];
res.push_back(node);
}
}
return res;
}
vector<TreeNode *> generateTrees(int n) {
return generateRes(1,n); }
};

  

Unique Binary Search Trees I&&II(II思路很棒)——动态规划(II没理解)的更多相关文章

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

  2. LeetCode:Unique Binary Search Trees I II

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

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

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

  4. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

  5. 96. Unique Binary Search Trees(I 和 II)

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

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

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

  7. 【leetcode】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. Unique Binary Search Trees I & II

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

随机推荐

  1. poj:1850 Code(组合数学?数位dp!)

    题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][ ...

  2. 四连测Day1

    题目:链接: https://pan.baidu.com/s/163ycV64ioy7uML7AvRDTGw 密码: p86i T1: 倍增求LCA,minn数组记录最小值 #include<i ...

  3. 【状压DP】【P2831】【NOIP2016D2T3】愤怒的小鸟

    传送门 Description Kiana 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 $(0,0)$ 处,每次 Kiana 可以用它向第一象限发射一 ...

  4. @RequestParam 注解的使用

    @RequestParam 注解的使用 前言 在SpringMvc后台进行获取数据,一般是两种. 1.request.getParameter(“参数名”) 2.用@RequestParam注解获取 ...

  5. Hdu3579 Hello Kiki

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. Star sky 二维前缀和

    C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  7. bzoj 2086 [Poi2010]Blocks 单调栈

    [Poi2010]Blocks Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 788  Solved: 356[Submit][Status][Dis ...

  8. Bigbluebutton安装过程

    BigBlueButton安装过程(翻译) 欢迎来到BigBlueButton 1.0-beta安装指南(以下简称BigBlueButton 1.0).BigBlueButton是一个开放源代码的网络 ...

  9. 【updating】python读书笔记-The Django Book2.0(for django1.4)

    原文:http://www.djangobook.com/en/2.0/frontmatter.html 译文:http://djangobook.py3k.cn/2.0/ 或者http://docs ...

  10. C++ 指针常见用法小结

    1. 概论 2.指针基础 3. 指针进阶 4. 一维数组的定义与初始化 5. 指针和数组 6. 指针运算 7. 多维数组和指针 8. 指针形参 9. 数组形参 10. 返回指针和数组 11. 结语   ...