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 ...
随机推荐
- HDU 4691(多校第九场1006) 后缀数组
...还能多说什么. 眼角一滴翔滑过. 一直以为题意是当前串与所有之前输入的串的LCP...然后就T了一整场. 扫了一眼标程突然发现他只比较输入的串和上一个串? 我心中突然有千万匹草泥马踏过. 然后随 ...
- MySQL 创建函数(Function)
目标 怎么样MySQL创建数据库功能(Function) 语法 CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,參数是可选的 RETUR ...
- ABAP函数:VIEW_MAINTENANCE_CALL(维护表视图等)
function:VIEW_MAINTENANCE_CALL 功能:维护表视图等 The function module calls the extended table maintenance (V ...
- tsunami:一种基于UDP协议的高速传输
一. 需求 近期在做数据库迁移.常常须要打包实例传输.传统scp感觉非常慢. 二. 软件信息 1. 软件主页:http://tsunami-udp.sf.net/ 2. 软件安装:直接源代码make ...
- 改动已有gpg密钥的用户标识及凝视
/********************************************************************* * Author : Samson * Date ...
- Cocos2d-x layout (二)
相对某个控件进行布局 Size widgetSize = Director::getInstance()->getWinSize(); Text* alert = Text::create(&q ...
- PAIP: Paradigms of Artificial Intelligence Programming
PAIP: Paradigms of Artificial Intelligence Programming PAIP: Paradigms of Artificial Intelligence Pr ...
- NET单元测试的艺术
NET单元测试的艺术 开篇:上一篇我们学习基本的单元测试基础知识和入门实例.但是,如果我们要测试的方法依赖于一个外部资源,如文件系统.数据库.Web服务或者其他难以控制的东西,那又该如何编写测试呢?为 ...
- dokcer 运行和进入容器
<pre name="code" class="html">docker:/root# docker run -itd --name zjtest8 ...
- 如何为linux释放内存和缓存
如何为linux释放内存和缓存_华陌飞尘_新浪博客 如何为linux释放内存和缓存 (2011-10-20 10:49:01) 标签: linux swap me ...