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

思路:dp。设数组res[i]表示n=i时的二叉搜索树个数。

先考虑最简单的情况,res[0] = 0,res[1] = 1。

当n > 1时,一定会存在这样两类二叉搜索树:root节点为1,以及root节点为n本身的树。当root节点为1时,剩下的(n - 1)个节点值全部大于1,都在右孩子的子树里,因此root节点为1的树的总数由规模为n - 1的右孩子子树决定,即个数等于res[n - 1]。当root节点为n时同理,个数也为res[n - 1]。

现在考虑其余节点为root时的情况,假设i为root节点,则值为1到i - 1的节点都会在左孩子子树中,值为i + 1到n的节点都会在右孩子子树中。

因此可能情况是res[i - 1] * res[n - i]。

综上,我们将所有节点为root时的值累加,即为大小为n的二叉搜索树的个数。

 class Solution {
public:
int numTrees(int n) {
vector<int> res(n + , );
res[] = ;
for (int i = ; i <= n; i++)
{
res[i] = res[i - ] * ;
for (int j = ; j < i; j++)
res[i] += res[j - ] * res[i - j];
}
return res[n];
}
};

Unique Binary Search Tree - Leetcode的更多相关文章

  1. Unique Binary Search Tree II

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

  2. [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

    本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...

  3. Validate Binary Search Tree [LeetCode]

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. Recover Binary Search Tree [LeetCode]

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. Validate Binary Search Tree——LeetCode

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. Recover Binary Search Tree leetcode java

    题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...

  7. Validate Binary Search Tree leetcode java

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  8. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  9. Lowest Common Ancestor of a Binary Search Tree -- LeetCode

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. RCP 主题切换

    第一步 编写css文件,放到项目目录下 第二步 添加切换主题扩展点 第三步 设置主题   public  void switchTheme(String themeID) {           Bu ...

  2. 15,scrapy中selenium的应用

    引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生如果直接用scrapy对其url发请求,是获取不到那部分动态加载出来的数据值,但是通过观察会发现,通过浏览器 ...

  3. Sicily 8843 Ranking and Friendship

    http://soj.me/8843 题意:几个人想做好朋友,朋友之间相差位置小于等于k,且长度相同分析:排序,将长度相同的放在一起.若长度相同,第i个人能放进去的条件是位置相差下雨等于k.      ...

  4. P2615 神奇的幻方

    P2615 神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首 ...

  5. mysql 分类

    一.系统变量 说明:变量由系统提供,不用自定义 语法: 1.查看系统变量 show[global | session]varisables like ‘ ’:如果没有显示声明global 还是sess ...

  6. bash shell命令与监测的那点事(二)

    bash shell命令与监测的那点事之top 上次我们说到了ps命令,ps命令虽然在收集运行在系统上的进程信息很有用,但是也有不足之处,ps命令只能显示某个特定时间点的信息,如果你想观察频繁换进换出 ...

  7. 位图 c++ 位图排序

    什么是位图?来自http://www.cnblogs.com/dolphin0520/archive/2011/10/19/2217369.html 位图就是用一个bit来标记某个元素对应的值,键值就 ...

  8. windows下命令行

    创建文件夹 mkdir 文件夹名字 创建文件 echo >文件名字 输入文件内容

  9. LAMP总四部分

    第一部分 1. 安装mysqlcd /usr/local/src/ 免安装编译二进制的包wget http://syslab.comsenz.com/downloads/linux/mysql-5.1 ...

  10. JZOJ 5281 钦点

    样例输入: 4 4 2 a a b b a a b b c c d d c c d d 1 1 3 3 2 2 3 1 1 3 2 2 样例输出: d d c c  d d c c  b b a a  ...