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. gulp 学习入门

    const gulp = require('gulp'); const less = require('gulp-less') // 定义任务 gulp.task('helloGulp',functi ...

  2. CodeForces-1100C NN and the Optical Illusion 简单数学

    题目链接:https://vjudge.net/problem/CodeForces-1100C 题意: 题目给出外部圆的数目n和内部圆的半径r,要求求出外部圆的半径以满足图片要求. 显然这是一道数学 ...

  3. 量化交易alpha、beta、shape等基本概念梳理

    1.期货型基金(CTA)的 Alpha 和 Beta 是指什么?  https://zhuanlan.zhihu.com/p/20700337 1980S                       ...

  4. R语言 批量下载财务报表

    getsheets <- function(symbol,type,file){ pre="http://money.finance.sina.com.cn/corp/go.php/v ...

  5. LGOJ1344 追查坏牛奶

    Description link 题意概述:给一张图,每条边有边权,求让点 \(1\) 和点 \(n\) 不连通的"最小破坏边权和" 和 "在此基础上的最小破坏边数&qu ...

  6. PowerShell-Selenium技术实时调试和操作Chrome浏览器

    只需要4行代码: $AnyWindow=$Chrome.WindowHandles.Item() $Chrome=$Chrome.SwitchTo().Window($AnyWindow) Write ...

  7. 关于ebay平台接口(php)对接示例

    获取订单接口示例 public function importEbayOrder(){ set_time_limit(0); if(empty( $this->_ShopApiEbay-> ...

  8. python妹子图爬虫5千张高清大图突破防盗链福利5千张福利高清大图

    meizitu-spider python通用爬虫-绕过防盗链爬取妹子图 这是一只小巧方便,强大的爬虫,由python编写 所需的库有 requests BeautifulSoup os lxml 伪 ...

  9. python代码实现购物车(django的redis与vue)

    安装模块 pip install django-redis 后端代码 # 购物车 class CartView(APIView): # 初始化函数 def __init__(self): self.c ...

  10. UserTokenManager JwtHelper

    package org.linlinjava.litemall.wx.service; import org.linlinjava.litemall.wx.util.JwtHelper; /** * ...