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. [转]玩转Google开源C++单元测试框架Google Test系列

    gtest的官方网站是: http://code.google.com/p/googletest/ 从官方的使用文档里,你几乎可以获得你想要的所有东西 http://code.google.com/p ...

  2. xml数据解析调研

    XML数据解析http://www.tuicool.com/articles/Nraau2(必用) http://www.cnblogs.com/pengyingh/articles/2342699. ...

  3. js获取URL地址中的GET参数

    var $_GET = (function(){ var url = window.document.location.href.toString(); var u = url.split(" ...

  4. android 之 XMLPull

    Pull解析 Pull的XML解析操作与SAX解析操作类似,也是采用事件驱动的方式.当XML文档开始解析或者遇到节点时都会有相应的事件代码触发. 主要涉及两个类: org.xmlpull.v1.Xml ...

  5. 将table导出为Excel的标准无乱码写法

    导出为Excel有很多种写法,对于一些复杂的格式,笔者喜欢在后台先拼成一个<table>,再使用Response输出. 如果数据中包含中文或者一些特殊字符,可很多不规范的写法都会导致页面乱 ...

  6. shell指令expr和test指令

    通过expr指令可以进行+.-.*.\.%等运算,但是有一点值得注意,使用乘法时,要在*前加上一个\符号. 通过test指令可以进行逻辑测试,进行测试的情况有四种: 1.整数测试 a.判断两个整数是否 ...

  7. SharePoint 2013 Nintex Workflow 工作流帮助(三)

    博客地址 http://blog.csdn.net/foxdave 工作流动作 3. Assign Flexi Task(Commonly used.User interaction分组) 看来是最常 ...

  8. php访问数据库思维导图

  9. hdu2196 树的直径 + bfs

    //Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...

  10. acm 20140825

    为了自己的梦想,一次次的选择坚强.走上acm这条路,怎么也找不到让自己放弃的理由.我喜欢这种竞赛的氛围,我渴望在赛场上飞扬!想想过去的一个学习,自己并没有干点什么有意义的事.acm也没有好好的做!新的 ...