Unique Binary Search Trees——LeetCode
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,输出它能有多少种表示二叉搜索树的形式。
解题思路:因为是给一个数字n,求它的所有的表示数量,可以简单考虑,设F[n]为n对应的总数。
假设n=4,那么以1为根,形式共有F[0]*F[3],F[0]和F[3]分别对应左右子树的数量;
以2为根,形式共有F[1]*F[2]种;
以3为根,形式共有F[2]*F[1]种;
以4为根,形式共有F[3]*F[0]种;
那么F[4]=F[0]*F[3]+F[1]*F[2]+F[2]*F[1]+F[3]*F[0],由此可以写出代码,由小到大推出来。
public int numTrees(int n) {
int[] dp = new int[n + 1];
Arrays.fill(dp, 0);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
dp[i] += dp[j - 1] * dp[i - j];
}
}
return dp[n];
}
Unique Binary Search Trees——LeetCode的更多相关文章
- Unique Binary Search Trees leetcode java
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- Unique Binary Search Trees [LeetCode]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- 【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
随机推荐
- 10.23 noip模拟试题
尼玛蛋pdf好难粘 直接写了 T1 /*开始写wa了 我真弱2333 关于p的排序规则不只是差值 为了字典序最小 还要拍别的*/ #include<cstdio> #include< ...
- 国内流行的两大开源.net微信公众平台SDK对比分析
最近忙于微信周边的开发 难免手痒去搜索一下有没有相关的sdk直接拿来使 还真发现了不少 这里总结两个看起来比较不错的.net平台下基于C#语言开发的SDK 一个强大一个小巧 (1) Senparc.W ...
- 【转】Windows环境下.NET 操作Oracle问题
目前,Windows操作系统可以分成两类,32位和64位(64位也区分x86_64位和Itanium ),同时Oracle客户端也做了同样的区分. 在安装和开发的过程中,经常会遇到一些问题,本文就总结 ...
- Switch Case语句中多个值匹配同一个代码块的写法
switch ($p) { case 'home': case '': $current_home = 'current'; break; case 'users.online': case 'use ...
- 受限玻尔兹曼机(RBM)
能量模型 RBM用到了能量模型. 简单的概括一下能量模型.假设一个孤立系统(总能量$E$一定,粒子个数$N$一定),温度恒定为1,每个粒子有$m$个可能的状态,每个状态对应一个能量$e_i$.那么,在 ...
- spring jdbctemplate调用procedure(返回游标)
package cn.com.git.htsc.uac.core.repository.report; import cn.com.git.htsc.uac.core.api.dto.report.R ...
- 【POJ2155】【二维树状数组】Matrix
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- javascript——面向对象程序设计(2)
<script type="text/javascript"> //1.理解原型对象 //2.原型与in操作符 //3.更简单的原型语法 //4.原型的动态性 //5. ...
- java编码转化方案-备用
import java.io.UnsupportedEncodingException; /** * 转换字符串的编码 */ public class changeCharSet { /** 7位AS ...
- Jqprint 轻量级页面打印插件
最近项目中需要在页面上添加一个打印的按钮,上网搜索了一下就发现了这个好用的超轻量插件,使用起来很方便 1.首先需要引入必须的js文件 <script language="javascr ...