Unique Binary Search Trees

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

Base case: n==0, n==1时,f(n)==1

递推关系:f(n)=∑f(i)*f(n-i-1)。即以第i个为根节点,左右子树数目相乘。

解法一:递归

class Solution {
public:
int numTrees(int n) {
if(n == )
return ;
else if(n == )
return ;
else
{
int count = ;
for(int i = ; i <= (n-)/; i ++)
{
if(i < n--i)
count += *numTrees(i)*numTrees(n--i);
else
count += numTrees(i)*numTrees(n--i);
}
return count;
}
}
};

解法二:动态规划

class Solution {
public:
int numTrees(int n) {
if(n== || n == )
return ; vector<int> v(n+, );
v[] = ;//n==0
v[] = ;//n==1
for(int i = ; i <= n; i ++)
{//n == i
for(int j = ; j < i; j ++)
{
v[i] += v[j]*v[i--j];
}
}
return v[n];
}
};

【LeetCode】96. Unique Binary Search Trees (2 solutions)的更多相关文章

  1. 【LeetCode】96. Unique Binary Search Trees 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 记忆化递归 动态规划 卡特兰数 日期 题目地址:ht ...

  2. 【LeetCode】96 - Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  4. 【一天一道LeetCode】#96. Unique Binary Search Trees

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  5. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  6. 【一天一道LeetCode】#95. Unique Binary Search Trees II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. LeetCode OJ 96. Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  8. [leetcode tree]96. Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  9. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. 如何理解Hibernate中的HibernateSessionFactory类

    package com.zz.util; import org.hibernate.HibernateException; import org.hibernate.Session; import o ...

  2. Debian学习笔记

    14.1. 禁止非root用户登录系统 在/etc目录下新建一个nologin文本文件,内容随意.当系统发现该文件,就会禁止其它用户登录,并显示该文件内容. 14.2. 禁用CTRL+ALT+DEL组 ...

  3. 微信开发之如何使用开发工具--weixin-java-tools

    一.前沿 微信公众平台由于没有提供针对语言的开发包,只公布了一个基于Http协议的接口和加解密的算法sdk,这样给微信公众号的开发者带来很多工作量,除了实现业务逻辑外,还需要自己处理底层的接口协议细节 ...

  4. poj 3258 River Hopscotch 题解

    [题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...

  5. Java 命名小技巧

    存储信息: xxxStorage 映射: xxxMapping 通过参数获取某个对象: getxxxFor 处理器: xxxHanlder handle 检索: xxxretriever 验证器: x ...

  6. SQL PRIMARY KEY,SQL FOREIGN KEY

    A primary key is defined as a column or a group of column that their value are always be unique. Nor ...

  7. mysql的安全性机制

    MySQL中主要包括两种用户:root用户和普通用户,其中前者为超级管理员,拥有MySQL提供的一切权限:而普通用户则只能拥有创建用户时赋予它的权限. MySQL的安全性机制主要包括权限机制,用户机制 ...

  8. text-overflow样式属性值ellipsis的用法

            一.div标签中使用text-overflow样式属性值ellipsis的方法: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1. ...

  9. ActiveMQ API 详解

    4.1 开发JSM的步骤 广义上说,一个JMS 应用是几个JMS 客户端交换消息,开发JMS 客户端应用由以下几步构成:      用JNDI 得到ConnectionFactory 对象:      ...

  10. Download Visual Studio

    Welcome to a new way to install Visual Studio! In our newest version, we've made it easier for you t ...