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. Compoer介绍

    Compoer介绍 Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 安装Composer Composer.phar 是 Compos ...

  2. Redis实现之RDB持久化(二)

    RDB文件结构 在Redis实现之RDB持久化(一)这一章中,我们介绍了Redis服务器保存和载入RDB文件的方法,在这一节,我们将对RDB文件本身进行介绍,并详细说明文件各个部分的结构和意义.图1- ...

  3. cacheData

    <%@ page language="java" import="java.util.*,com.fiberhome.bcs.appprocess.common.u ...

  4. Windows 2008 WinRM和WinRS能帮你做什么?

     介绍    WinRM及WinRS更新包含在Windows Vista, Windows Server 2003 R2, Windows Server 2008 (及 Server 2008 Cor ...

  5. IOS开发学习笔记009-OC基本知识

    开始学习OC,时间不等人啊,要抓紧了. OC基本知识 新建一个以.m结尾的文件,这既是oc的程序文件.在oc程序中完全兼容C语言.编译好链接类似. oc包含头文件是使用#import <> ...

  6. Monkey日志信息的11种Event percentages

    我们查看官方文档,表里只给出了8种事件(可以看我上篇的翻译文档).但我们运行Monkey后,却发现有11种事件!最坑爹的是,在每种事件的百分比后面,他还不给注明是什么事件! 原来不同的Android ...

  7. 【转】Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)

    本篇文章主要介绍了"Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)",主要涉及到Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)方 ...

  8. Ubuntu系统设置中心不见了

    sudo apt-get install unity-control-center 执行安装上述即可 原因是因为在安装搜狗输入法的时候卸载了ibuts与系统控制中心

  9. 【bzoj2179】FFT快速傅立叶 FFT

    题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出 输出一行,即x*y的结果. 样例 ...

  10. Markdown语法图解

    Markdown语法图解 文章目录 快捷键 基本语法 对字体设置斜体.粗体.删除线 分级标题 链接 分割线 代码块 引用 列表 表格 常用技巧 换行 缩进字符 如何打出一些特殊符号 字体.字号与颜色 ...