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. linux命令行快捷键

    linux命令行编辑快捷键 先总结几个个人觉得最有用的 ctrl + ? 撤消前一次输入 ctrl + c 另起一行 ctrl + r 输入单词搜索历史命令 ctrl + u 删除光标前面所有字符相当 ...

  2. Eclipse 汉化包

    http://www.eclipse.org/babel/downloads.php 下载地址,具体操作请百度. http://subclipse.tigris.org/update_1.6.x SV ...

  3. mysql 远程连接速度慢的解决方案

    PHP远程连接MYSQL速度慢,有时远程连接到MYSQL用时4-20秒不等,本地连接MYSQL正常,出现这种问题的主要原因是,默认安装的MYSQL开启了DNS的反向解析,在MY.INI(WINDOWS ...

  4. spring mvc环境配置

    spring mvc将所有的请求都经过一个servlet控制器-DispatcherServlet,这个servlet的工作就是将一个客户端的request请求分发给不同的springmvc控制器,既 ...

  5. java反射之Class.getMethod与getDeclaredMethods()区别

    Class对象的getMethods和getDeclaredMethods都是获取类对象的方法.但是又有所不同.废话不多说, 先看demo package com.westward; public c ...

  6. bzoj 2561: 最小生成树

    #include<cstdio> #include<iostream> #include<cstring> #define M 100009 #define inf ...

  7. 存储过程Oracle学习(一)

    一.简介 存储过程:就是在数据库中创建的一段程序,供别人调用 .其实我感觉跟定义一个方法相似 二.无参存储过程 如下,经典的输出"Hello World"来入门存储过程 创建一个存 ...

  8. HDU 3966 基础树链剖分

    题意:给一棵树,并给定各个点权的值,然后有3种操作:I C1 C2 K: 把C1与C2的路径上的所有点权值加上KD C1 C2 K:把C1与C2的路径上的所有点权值减去KQ C:查询节点编号为C的权值 ...

  9. 踏着前人的脚印学Hadoop——结构、重点

    HDFS作为一个分布式文件系统,是所有这些项目的基础.分析好HDFS,有利于了解其他系统.由于Hadoop的HDFS和MapReduce是同一个项目,我们就把他们放在一块,进行分析. 如果把整个had ...

  10. javaweb之Cookie篇

    Cookie是在浏览器访问某个Web资源时,由Web服务器在Http响应消息头中通过Set-Cookie字段发送给浏览器的一组数据. 一个Cookie只能表示一个信息对,这个信息对有一个信息名(Nam ...