Unique Binary Search Trees In JAVA
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
public class Solution {
public int numTrees(int n) {
if(n==0||n==1)
return 1;
int res=0;
for(int i=1;i<=n;i++)
{
res+=numTrees(i-1)*numTrees(n-i);
}
return res;
}
}
Unique Binary Search Trees In JAVA的更多相关文章
- leetcode 95 Unique Binary Search Trees II ----- java
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
- Unique Binary Search Trees leetcode java
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- Java for LeetCode 095 Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- Unique Binary Search Trees II leetcode java
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- LeetCode: Unique Binary Search Trees II 解题报告
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- LeetCode - Unique Binary Search Trees II
题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...
- 96. Unique Binary Search Trees
题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For e ...
- [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆
Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...
随机推荐
- html5前端开发笔记-个人中心
简单的css自适应 PC端 *** 移动端 *** ) *** 一开始的想法就是模仿手机APP 的页面进行布局,首先得有个头部,然后是主题部分,然后加上2个按钮,分别是编辑和退出登录.先布出基本结构. ...
- UIView的动画之初步学习
animateWithDuration:<#(NSTimeInterval)#> delay:<#(NSTimeInterval)#> options:<#(UIView ...
- 第1个linux命令——echo
功能:在显示器上显示一段文字,一般起到一个提示的作用. 语法:echo [-ne][字符串] 或 echo [--help][--version] 详细说明:echo会将输入的字符串送往标准 ...
- 13年山东省赛 Mountain Subsequences(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mountain Subsequences Time Limit: 1 Sec ...
- 求a,b在区间上的公倍数个数
给你两个数 a,b.问你区间 [1,N]中都是有多少个数是a,b的公倍数.当数据很大的时候,遍历肯定会超时.其实,我们可以首先求出 lcm(a,b).因为我们知道(a,b)公倍数都是它最小公倍数的倍数 ...
- HDU 1556 Color the ball - from lanshui_Yang
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a ...
- ASPCMS 多条件查询
1. 表单样例: <form name="topFrm" id="topFrm" action="/search.asp"> & ...
- 存储过程使用表变量或临时表代替游标Fetch实例,访问远程数据库
定义表变量是可以直接操作在内存中的数据,比较快.临时表在大数据量时会比游标使用的资源少.还是要看具体情况了.也有可能在实际优化过程中相互替换呢. 留作记忆的代码如下: if object_id('te ...
- Javascript经典实例 - 正则表达式
1] 正则常用字符 正则表达式常用的特殊字符 字符 匹配 示例 ^ 匹配输入的开始 /^This/匹配 “This is ...” $ 匹配输入的结束 /end$/匹配“This is the end ...
- ipad安装自制ipa
自己用XCode写了个小程序,想打包成ipa安装在真机上,网上查了查: 1.将工程的编译版本设置为release(在edit scheme里): 2.build for Archiving(Produ ...