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. bzoj 5055: 膜法师——树状数组

    Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然,他能为长者所续的时间,为这三个维度上能量的乘 ...

  2. js延迟几秒执行

    var t=setTimeout("location.reload()",5000);

  3. HDU 1073 Online Judge (字符串处理)

    题目链接 Problem Description Ignatius is building an Online Judge, now he has worked out all the problem ...

  4. 添加 MySql 服务、Tomcat服务到windows服务中

    添加 MySql 服务到windows服务中: cmd --> F:\MySql\MySqlServer5.1\bin\mysqld --install 这样用默认的 MySQL 为名称添加一个 ...

  5. 查看 CUDA cudnn 版本

    cuda 版本 cat /usr/local/cuda/version.txt cudnn 版本 cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MA ...

  6. 使用Picker的时候,让input输入框使用焦点,手机键盘不弹出

    $("#address").click(function(){ document.activeElement.blur(); })

  7. 利用Volatility对Linux内存取证分析-常用命令翻译

    命令翻译 linux_apihooks - 检查用户名apihooks linux_arp - 打印ARP表 linux_aslr_shift - 自动检测Linux aslr改变 linux_ban ...

  8. 解决修改表结构,添加外键时出现“约束冲突”的错误

    由于表结构更改,使用新建表,现有部分表需要更改外键,将引用更改到新建表的相应字段.在更改过程中,部分表出现如下错误提示: ALTER TABLE 语句与 COLUMN FOREIGN KEY 约束 ' ...

  9. cobbler 无人值守系统安装

    概述 本文适合centos6 | centos7 系统的安装 执行操作之前:检查系统防火墙,selinux是否关闭,网络链接是否畅通. Cobbler是一个免费开源系统安装部署软件,用于自动化网络安装 ...

  10. 五、springcloud之客户端负载均衡Ribbon

    一.简介 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式: 一种是ribbon+restTemplate, ...