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 ...
随机推荐
- Redis:安装、配置、操作和简单代码实例(C语言Client端)
Redis:安装.配置.操作和简单代码实例(C语言Client端) - hj19870806的专栏 - 博客频道 - CSDN.NET Redis:安装.配置.操作和简单代码实例(C语言Client端 ...
- NFS服务器端配置
服务器端配置1 创建共享目录# mkdir /home/share# chown nobody.nogroup /home/share2 创建或修改/etc/exports 配置文件这个文件的内容非常 ...
- maven的pom 提示错误 Failure to transfer com.thoughtworks.xstream:xstream:jar:
pom文件提示错误,信息如下 Description Resource Path Location TypeFailure to transfer com.thoughtwor ...
- 编写在浏览器中不弹出警告的ActiveX控件
我们在编写ActiveX控件时,如果用在浏览器中,经常都会弹出现在运行的脚本不安全的提示, 如果给客户使用,将会带来极大不便.按照MSDN的介绍通常有两种一种是实现IObjectSafe接口,一种是通 ...
- dm642在线写EPROM.txt
void wirteEPROM() { //#include <stdio.h> unsigned short bufeprom[30],i,val; FILE *fp; ...
- ZenCoding Syntax
语法: 后代:> 缩写:nav>ul>li 兄弟:+ 缩写:div+p+bq 上级:^ 缩写:div+div>p>span+em^bq 缩写:div+div>p&g ...
- Ogre嵌入MFC傻瓜全然教程(三)
经过前两两篇博文的解说.我们已经完毕了渲染工作,但仅仅是渲染而没有交互性,本篇博文我们就来加上事件的处理方法. 首先我们须要为项目加入一个帧监听类:CMyFrameListener,为了直观,在这直接 ...
- 树莓派玩耍笔记4 -- 树莓派ssh党必备的配置
1. 关闭桌面显示 对于ssh 党.当然不须要系统花费资源在显示上. 所以我们先在 "raspi-conifg" 下选择默认启动为Text 启动(这好像也是Raspbian 的默认 ...
- C / C++算法学习笔记(7)-双向冒泡
原始地址:双向冒泡 通常的冒泡是单向的,而这里是双向的,也就是说还要进行反向的工作. 代码看起来复杂,仔细理一下就明白了,是一个来回震荡的方式. 写这段代码的作者认为这样可以在冒泡的基础上减少一些交换 ...
- Pyhon安装media模块
都是教科书惹的祸,它没有说清楚.media看着很标准,其实不是python自带的库.需要安装第三方软件后才能用. 在这里http://pythonhosted.org/PyGraphics/insta ...