Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树
Unique Binary Search Trees:求生成二叉排序树的个数。
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
算法分析:类似上阶梯,简单的动态规划问题。当根节点为i时,比i小的节点有i-1个,比i大的节点有n-i个,所以,i为根节点能够生成二叉排序树的个数是
nums[n] += nums[i-1]*nums[n-i],i从1到n。
public class UniqueBinarySearchTrees
{
public int numTrees(int n)
{
if(n <= 0)
{
return 0;
}
int[] res = new int[n+1];
res[0] = 1;
res[1] = 1;
for(int i = 2; i <= n; i ++)
{
for(int j = 1; j <= i; j ++)//j为根节点
{
res[i] += res[j-1]*res[i-j];
}
}
return res[n];
}
}
Unique Binary Search Trees2:求生成二叉排序树的根节点的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1...n.
For example,
Given n = 3, your program should return all 5 unique BST's shown below.
1 3 3 2 1
\ / / / \ \
3 2 1 1 3 2
/ / \ \
2 1 2 3
算法分析:这个不是求个数,而是求生成树根节点。使用递归。
public class UniqueBinarySearchTreesII
{
public List<TreeNode> generateTrees(int n)
{
if(n <= 0)
{
return new ArrayList<TreeNode>();
} return helper(1, n);
} public List<TreeNode> helper(int m, int n)
{
List<TreeNode> res = new ArrayList<>();
if(m > n)
{
res.add(null);
return res;
} for(int i = m; i <= n; i ++)
{
//i为根节点
List<TreeNode> ls = helper(m, i-1);//i节点的左子树
List<TreeNode> rs = helper(i+1, n);//i节点的右子树
for(TreeNode l : ls)
{
for(TreeNode r : rs)
{
TreeNode curr = new TreeNode(i);
curr.left = l;
curr.right = r;
res.add(curr);
}
}
}
return res;
}
}
Unique Binary Search Trees,Unique Binary Search Trees2 生成二叉排序树的更多相关文章
- 41. 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 ...
- Unique Binary Search Trees,Unique Binary Search Trees II
Unique Binary Search Trees Total Accepted: 69271 Total Submissions: 191174 Difficulty: Medium Given ...
- 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 && Unique Binary Search Trees II
1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...
- 将百分制转换为5分制的算法 Binary Search Tree ordered binary tree sorted binary tree Huffman Tree
1.二叉搜索树:去一个陌生的城市问路到目的地: for each node, all elements in its left subtree are less-or-equal to the nod ...
- [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 ...
- 2 Unique Binary Search Trees II_Leetcode
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- 找新朋友---hdu1286(欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286 欧拉函数:对正整数n,欧拉函数是求少于n的数中与n互质的数的数目: 素数(质数)指在一个大于1的 ...
- (0.2.6)Mysql安装——编译安装
(0.2.6)Mysql安装——编译安装 待完善
- 以EJB谈J2EE规范
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiaoduishenghuogo/article/details/24800703 接触J2EE的时 ...
- sscanf,sscanf
在#include<stdio.h> 定义函数 int sscanf (const char *str,const char * format,........); 函数说明: sscan ...
- Hadoop2.7.3+spark2.1.0+hbase0.98分布式集群部署
运行环境配置(10.79.115.111-114) 1.关闭防火墙与selinux 关闭防火墙: CentOS 7.x默认使用的是firewall作为防火墙. systemctl stop firew ...
- SpringMVC的其他功能使用
一.SpringMVC支持在控制器的业务方法中写入参数作为传递过来的变量 @Controller @RequestMapping(value="/kaiye") public cl ...
- 理解ASM的Extent
理解ASM的Extent 分类: Oracle 2017-04-14 10:19:44 ASM中分配空间的单位是AU,Extent包含1个或多个AU.在11g之前,1个Extent对应1个AU.而 ...
- matplotlib绘制饼状图
源自http://blog.csdn.net/skyli114/article/details/77508430?ticket=ST-41707-PzNbUDGt6R5KYl3TkWDg-passpo ...
- 工作笔记-javascript-网络层封装
/** * @Author Mona * @Date 2016-12-08 * @description 网络层封装 */ /** * 封装基本请求方式 */ window.BaseRequest = ...
- python全栈开发从入门到放弃之初识面向对象
面向过程 VS 面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了写程序的复 ...