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. oracle数据泵备份(Expdp命令)[转]

      Oracle备份方式主要分为数据泵导出备份.热备份与冷备份三种,今天首先来实践一下数据泵备份与还原.数据泵导出/导入属于逻辑备份,热备份与冷备份都属于物理备份.oracle10g开始推出了数据泵( ...

  2. 指定socket文件连接mysql

    1.利用ps aux |grep mysql  查看--socket 路径 2.创建软连接.创建文件 3.登录成功

  3. Spring mvc中使用request和response

    @ResponseBody @RequestMapping(value="/ip", method=RequestMethod.GET) public String getIP(H ...

  4. 使用URL读取网络资源

    import java.io.InputStream;import java.io.OutputStream;import java.net.URL; import android.os.Bundle ...

  5. S1:函数上下文

    函数的上下文是可以变化的,因此,函数内的this也是可以变化的,函数可以作为一个对象的方法,也可以同时作为另一个对象的方法,总之,函数本身是独立的.可以通过Function对象上的call或者appl ...

  6. 1800: [Ahoi2009]fly 飞行棋

    #include<cstdio> #include<iostream> using namespace std; ],n,ans; int main() { scanf(&qu ...

  7. C#窗体的加载等待(BackgroundWorker控件)实现

    窗体拉一个Button按钮和一个加载等待显示的label, label默认隐藏,点击按钮时显示这个label,加载完再隐藏 1.工具箱拉BackgroundWorker控件到窗体 2.backgrou ...

  8. cssTex

    var head= document.getElementById("head");head.style.cssText="width:200px;height:70px ...

  9. [开发笔记]-FireWorks常用操作快捷键

    一.工具快捷键 指针.选择后方对象[V],[0] 部分选定[A],[1] 选取框.椭圆选取框[M] 套索.多边形套索[L] 裁剪.导出区域[C] 魔术棒[W] 线条工具[N] 钢笔工具[P] 矩形.圆 ...

  10. centos7 学习1 KDE配置中文

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