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
 
思路:
遇到二叉树,多半是分为左右子树两个问题递归下去。
由序列[1,…,m]和[i+1,…,i+m]能够构造BST的数目是相同的,不妨将原问题转化为求n个连续的数能够构成BST的数目
考虑分别以1,2,…i…,n为root,左子树由1~i-1这i-1个数构成,右子树有i+1~n这n-i-2个数构成,左右子树数目相乘即可,然后再累加
 
代码:
 int numUniTrees(int n, vector<int> &res){
if(res[n] != -)
return res[n]; if(n == ||n == ){
res[n] = ;
return res[n];
} int num = ;
for(int i = ; i < n; i++){
//num += numTrees(i)*numTrees(n-i-1);//剥离出来一个函数之后,注意更改函数名
num += numUniTrees(i, res) * numUniTrees(n-i-, res);
}
res[n] = num;
return res[n];
}
int numTrees(int n) {
if(n < )
return ;
vector<int>res(n+, -);
return numUniTrees(n, res);
}

【题解】【BST】【Leetcode】Unique Binary Search Trees的更多相关文章

  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. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

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

  3. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

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

  4. LeetCode: Unique Binary Search Trees II 解题报告

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

  5. LeetCode - Unique Binary Search Trees II

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

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

  7. [leetcode]Unique Binary Search Trees @ Python

    原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...

  8. LEETCODE —— Unique Binary Search Trees [动态规划]

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

  9. Leetcode Unique Binary Search Trees

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

  10. LeetCode: Unique Binary Search Trees [095]

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

随机推荐

  1. Java:数组

    数组 数组是一种引用数据类型(所以才会看到 new int[]),数组的长度初始化完成后是固定的.在内存中初始化数组后的空间就固定下来,即便数组中的内容被清空了,但在内存中占有的空间保留了下来,依然是 ...

  2. [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想

    Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...

  3. [js]多个物体的运动

    与单个的区别:得知道哪个在动,所以运动函数需要两个参数,出了目标iTarget之外,还要obj.另外需要多个计数器,否则当一个还没运动完就移入另一个物体会发生卡壳 window.onload=func ...

  4. 类成员函数作为pthread_create函数参数

    from:http://www.cnblogs.com/shijingxiang/articles/5389294.html 近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数 ...

  5. tarjan求强联通分量 模板

    void tarjan(int u) { dfn[u]=low[u]=++dfs_clock; stack_push(u); for (int c=head[u];c;c=nxt[c]) { int ...

  6. ros使用RPLIDAR激光雷达

    1.首先下载RPLIDAR的驱动功能包 https://github.com/robopeak/rplidar_ros 2.然后解压放到~/catkin_ws/src目录下 3.执行catkin_ma ...

  7. html5+js实现刮刮卡效果

    通过Canvas实现的可刮涂层效果. 修改img.src时涂层也会自动适应新图片的尺寸. 修改layer函数可更改涂层样式. 涂层: 可刮效果: <!DOCTYPE html> <h ...

  8. centos7 学习1 KDE配置中文

    安装kde桌面后没有中文,可以用以下方法配置中文 #yum list kde*chinese 会显示可以安装的包,我的显示如下 kde-l10n-Chinese.noarch -.fc14 @upda ...

  9. HDU5478 原根求解

    看别人做的很简单我也不知道是怎么写出来的 自己拿到这道题的想法就是模为素数,那必然有原根r ,将a看做r^a , b看做r^b那么只要求出幂a,b就能得到所求值a,b 自己慢慢化简就会发现可以抵消n然 ...

  10. POJ 1739

    楼教主男人八题之一... 题目大意: 求从左下角经过所有非障碍点一次到达右下角的方案数 这里不是求回路,但是我们可以考虑,在最下面一行再增加一行,那么就可以当做求此时左下角到右下角的回路总数,那么就转 ...