LeetCode: Unique Binary Search Trees [095]
【题目】
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,2,3,4,5...n这n个值,能构造多少棵合法的二叉搜索树
【思路】
对于给定[1,n]区间,先确定全部可能的根。如果根为k, 则该二叉树左子树的取值区间为[1, k-1]和右子树的取值区间[k+1, n]。
而二叉搜索搜索树的随意一个节点的左右子树也是二叉搜索树,因此我们须要确定[1,k-1]和[k+1, n]上构造的二叉搜索树的数目, 比方分别为left[k], right[k]。
则以k的根的二叉搜索树的数目即为,left[k]*right[k]
本题用递归来解决。
【代码】
class Solution {
public: int binaryTreeNums(int start, int end){
//[start, end]区间上构造二叉树的数目
if(start>=end)return 1; //start<end表示空子树, start==end表示叶子节点 int treeNums=0; for(int root=start; root<=end; root++){
int leftCount = binaryTreeNums(start, root-1); //计算左子树的数目
int rightCount = binaryTreeNums(root+1, end); //计算右子树的数目
treeNums+= leftCount*rightCount;
} return treeNums;
} int numTrees(int n) {
if(n==0)return 0;
return binaryTreeNums(1, n);
}
};
LeetCode: Unique Binary Search Trees [095]的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- [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 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 ...
- Leetcode:Unique Binary Search Trees & Unique Binary Search Trees II
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
- [leetcode]Unique Binary Search Trees @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- 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
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- uva 12096
优先队列,主要是STL应用所以复制一下 #include <iostream> #include <cstdio> #include <cstdlib> #incl ...
- OpenStack使用Bosh部署CloudFoundry(一)—准备OpenStack环境
版本说明: CloudFoundry:V2版本 OpenStack:Folsom或者Grizzly版本 本篇文章采用OpenStack Folsom+nova-network的OpenStack环境, ...
- read table 时关键字TRANSPORTING NO FIELDS的用法
关键字TRANSPORTING NO FIELDS 用于read table with key 一般用于等读取内表的时候,只是判断该内表中是否有次数据 不需要读取到工作区中. READ TABLE g ...
- JSTL解析——002——core标签库01
javaEE5之前的版本需要引用JSTL相关的jar包.tld文件等,JAEE5之后就不用这么麻烦了, 如果你的还是不能使用就去官网下载(jstl.jar和standard.jar)这两个jar包,将 ...
- Tokyo Tyrant(TTServer)系列(四)-tcrmgr远程管理与调试
Tokyo Tyrant(TTServer)系列-tcrmgr(远程管理与调试) tcrmgr是TokyoTyrant的管理工具,对ttserver进行管理与执行命令: 通过输入tcrmgr回车,能够 ...
- 条款38 通过复合塑膜出has-a或"依据某物实现"
结论: 复合的意义和public继承全然不同. (public继承參考:条款32 确定你的public继承塑模出is-a关系) 在应用域,复合意味着has-a(有一个).在实现域,复合意味着is-im ...
- Delphi 数据类型列表 good
Delphi 数据类型列表 分类 范围 字节 备注 简单类型 序数 整数 Integer -2147483648 .. 2147483647 4 有符号32位 Cardinal 0 .. 429496 ...
- Android installed app, never used, cannot receiver BroadcastReceiver
官方文档是这么写的:(http://developer.android.com/about/versions/android-3.1.html#launchcontrols) Launch contr ...
- javascript 下拉列表 自己主动取值 无需value
<select id="applyType" name="$!{status.expression}" class="inp" onc ...
- form不提交问题
var confirmOrderForm=document.getElementById("confirmOrderForm"); var url="${pageCont ...