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

给出一个n,求1-n个数可以组成多少种二叉查找树

思路:对每一个root为k的树有多少种来说,那就就需要知道左子树的根k-1可以组成多少种树,同时右子树根为n-1-k时候有多少种树,结果就是左边和右边的种类数相乘

动态规划

 class Solution(object):
def numTrees(self, n):
flag = [0]*(n+1)
flag[0],flag[1] = 1,1
for i in range(2,n+1):
for j in range(i):
flag[i] += flag[j]*flag[i-1-j]
return flag[n]

[leetcode tree]96. Unique Binary Search Trees的更多相关文章

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

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

  2. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

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

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

  4. 【LeetCode】96 - Unique Binary Search Trees

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

  5. [leetcode tree]95. Unique Binary Search Trees II

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

  6. 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 ...

  7. [LeetCode] 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 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses

    96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...

  9. 52. leetcode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

随机推荐

  1. 【BearChild】

    \(≧▽≦)/ BearChild is salty. If you want to save him,please call QQ:423339833 to talk♂with him. Have ...

  2. js操作控制iframe页面的dom元素

    1.代码1  index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  3. Linux基础-rpm软件包管理

    任务:挂载光盘文件到/media目录,进去/media目录下的Packages目录,查看系统已安装的所有rpm包,查看系统是否安装dhcp软件包,安装dhcp软件包,查看dhcp软件包的信息,查看dh ...

  4. Postman和Selenium IDE开局自带红蓝BUFF属性,就问你要还是不要

    话不多说,下面给大家介绍两款工具,selenium IDE和Postman. 为什么说是自带红蓝Buff,因为想做UI自动化和接口自动化的同学,很多时候,都难在了开头. 比如你要学习语言,你要学习框架 ...

  5. 002_安装第三方APP好的站点及解除安全与隐私限制

    一.解除安全与隐私限制的任何来源. http://bbs.feng.com/read-htm-tid-10714286.html 接下来,我们就打开终端,然后输入以下命令:   sudo spctl ...

  6. go 切片的 插入、删除

    package main import ( "fmt" ) func InsertSpringSliceCopy(slice, insertion []string, index ...

  7. Linux下配置Samba服务器全过程

    Linux下配置Samba服务器全过程 user级别的samba的配置 http://www.linuxidc.com/Linux/2014-11/109234.htm http://www.linu ...

  8. Git系统学习网址

    https://code.csdn.net/help/CSDN_Code/progit/zh/07-customizing-git/01-chapter7

  9. HDU 3068 最长回文(manacher模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求字符串s中最长的回文子串 解题思路:manacher模板 代码 #include&l ...

  10. HDU 2609 How many(最小表示+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609 题目大意: 题目大意有n个有01组成的字符串,每个字符串都代表一个项链,那么该字符串就是一个环状 ...