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

Example:

Input: 3
Output: 5
Explanation:
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
class Solution {
public int numTrees(int n) {
int[] nums = new int[n + 1];
nums[0] = 1;
nums[1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 0; j < i; j++) {
// use j - 1 b/c root count 1, left + right + cur_root = total number
nums[i] += nums[i - j - 1] * nums[j];
}
}
return nums[n];
}
}

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

  1. 52. leetcode 96. Unique Binary Search Trees

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

  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 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]* ...

  4. 96. Unique Binary Search Trees

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

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

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

  6. [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆

    [Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...

  7. 96. Unique Binary Search Trees (Tree; DP)

    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 (2 solutions)

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

  9. 96. Unique Binary Search Trees(I 和 II)

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

随机推荐

  1. 百度链接提交-js代码推送批量推送版

    1原百度JS链接推送代码 首先我们来看一下原百度JS链接推送代码是这*样的 用百度自己的话讲:JS链接推送代码以网页为最小对象,服务于全平台多终端,PC站和移动站均可使用.安装代码的页面在任意平台(浏 ...

  2. 使用Dom4j生成xml文件(utf-8编码)

    xml文件内容: <?xml version="1.0" encoding="UTF-8"?> <result> <code> ...

  3. 网鼎杯-Fakebook-反序列化和SSRF和file协议读取文件

    0x00知识点:SSRF SSRF (Server-side Request Forge, 服务端请求伪造) 是一种由攻击者构造形成由服务端发起请求的一个安全漏洞.一般情况下,SSRF攻击的目标是从外 ...

  4. 3D打印前途光明,它需要怎样的进化?

    在很长一段时间内,笔者都认为3D打印只会存在于科幻场景内,众多的科技大佬在前几年也和我保持相当一致的看法,代工大王郭台铭曾口出狂言:如果3D打印能够普及,我就把"郭"字倒过来写,时 ...

  5. Iptables的规则语法

    Iptables的规则语法 分类: 防火墙2012-04-19 17:09 1228人阅读 评论(0) 收藏 举报 inputtcpfilter防火墙output网络 (一) 基本语法 iptable ...

  6. art-template常用总结

    高性能JavaScript模板引擎原理解析:http://cdc.tencent.com/2012/06/15/%E9%AB%98%E6%80%A7%E8%83%BDjavascript%E6%A8% ...

  7. 安装使用离线版本的维基百科(Wikipedia)

    1 相关背景 平常大家在上网查询一些基本概念的时候常常会参考维基百科上面的资料,但是由于方校长研制的GFW(长城防火墙系统)强大的屏蔽功能,好多链接打开以后,不出意外会出现著名的“404NOT FOU ...

  8. 使用pyintaller打包python3.6项目,并用c#调用该脚本

    一.pythoninstaller 打包python项目 前提:安装python3.6环境+pycharm 1. 安装pyinstaller pip install pyinstaller 2. cm ...

  9. c# 基础switct---case用于处理多条件的定值的判断

    题目:李四的年终工作评定,如果定为A级,则工资涨500元,如果定为B级,则工资涨200元,如果定为C级,工资不变,如果定为D级工资降200元,如果定为E级工资降500元. 设李四原工资为5000,请用 ...

  10. JS—DOM操作

    节点分为三类: 1.元素节点:标签<div></div> 2.文本节点:标签内的纯文本. 3.属性节点:标签内的属性,id或class 查找元素: getElementById ...