leetcode — unique-binary-search-trees
/**
* Source : https://oj.leetcode.com/problems/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
*
*
*/
public class UniqueBinarySearchTree {
/**
* 求出给定n的所有唯一的二叉搜索树
* 二叉搜索树:
* 如果左子树不为空,则左子树的节点值要小于根节点的值,如果右节点不为空,则右子树节点的值大于根节点的值
*
* 找规律
* 设n的唯一二叉搜索树个数为f(n)
* n = 0, f(0) = 1
* n = 1, f(1) = 1
* n = 2, f(2) = 2
* n = 3, f(3) = 5
* .....
*
* 讨论n = 3的情况
* 当根节点为1,左子树必须为空,则总数取决于右子树的个数,f(0)*f(n-1)=f(0)*f(2)=2
* 当根节点为2,左子树为1,右子树为3,在总数为f(1)*(n-2)=f(1)*(1)=1
* 当根节点为3,左子树为空,可能有f(0)*f(2),左子树为1,可能有f(1)*f(1),左子树为2,可能有f(2)*f(0)=2,总共有2+1+2=5
*
* 所以:
* f(0) = 1
* f(n) = f(0)*f(n-1) + f(1)f(n-2) + f(2)*f(n-3) + ... + f(n-3)f(2) + f(n-2)*f(1) + f(n-1)f(0);
*
* 注意:因为要计算f(n),所以数组长度应该为n+1,因为要保存0-n之间的结果
*
*
* @param n
* @return
*/
public int uniqueTreeCount (int n) {
if (n == 0) {
return 1;
}
int[] dp = new int[n+1];
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
dp[i] += dp[j] * dp[i-j-1];
}
}
return dp[n];
}
public static void main(String[] args) {
UniqueBinarySearchTree uniqueBinarySearchTree = new UniqueBinarySearchTree();
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(0));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(1));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(2));
System.out.println(uniqueBinarySearchTree.uniqueTreeCount(3));
}
}
leetcode — unique-binary-search-trees的更多相关文章
- 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 ...
- LeetCode: Unique Binary Search Trees [095]
[题目] Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
随机推荐
- Cause: java. lang.InstantiationException: tk.mybatis.mapper.provider.base.BaseInsertProvider
相信现在Java Web开发都是用的mybatis吧,而用到mybatis很多人都不会错过通用mapper吧! (纯属瞎扯淡...qwq). 如我上一篇博客所写,目前公司新项目,使用了通用mapper ...
- Oracle (分类、数据库类型、序列)
分类: 1.DDL (定义语句) create .alter .drop 不需要commit create table aaa( tt1 varchart ) 2. DML (操纵语句) lnset ...
- oracle启动服务和监听
1.故障问题:tomcat显示启动oracle数据库失败,数据库服务启动正常 操作1:重启tomcat查看错误信息 2:重启数据库服务 命令: (1) 启动Oracle服务 C:\Users\Admi ...
- 回顾4180天在腾讯使用C#的历程,开启新的征途
今天是2018年8月8日,已经和腾讯解除劳动关系,我的公司正式开始运营,虽然还有很多事情需要理清,公司官网也没有做,接下来什么事情都需要自己去完成了,需要一步一个脚印去完善,开启一个新的征途,我将在博 ...
- 算法与数据结构(六) 迪杰斯特拉算法的最短路径(Swift版)
上篇博客我们详细的介绍了两种经典的最小生成树的算法,本篇博客我们就来详细的讲一下最短路径的经典算法----迪杰斯特拉算法.首先我们先聊一下什么是最短路径,这个还是比较好理解的.比如我要从北京到济南,而 ...
- .NET Core跨平台的奥秘[下篇]:全新的布局
从本质上讲,按照CLI规范设计的.NET从其出生的那一刻就具有跨平台的基因,这与Java别无二致.由于采用了统一的中间语言,微软只需要针对不同的平台设计不同的虚拟机(运行时)就能弥合不同操作系统与处理 ...
- 【RL-TCPnet网络教程】第21章 RL-TCPnet之高效的事件触发框架
第21章 RL-TCPnet之高效的事件触发框架 本章节为大家讲解高效的事件触发框架实现方法,BSD Socket编程和后面章节要讲解到的FTP.TFTP和HTTP等都非常适合使用这种方式 ...
- JavaScript基础系列
JavaScript基础系列 JavaScript是一种基于对象和事件驱动的客户端脚本语言. JavaScript的注释 // 单行 /**/ 多行注释 JavaScript变量,函数名和操作符都是区 ...
- 在vue中使用svg sprite
概述 这几天研究了一下在vue中使用svg sprite,有些心得,记录下来,供以后开发时参考,相信对其它人也有用. 在vue中导入svg 在vue中导入svg的方法有很多种,比如在img标签的src ...
- [Swift]LeetCode839. 相似字符串组 | Similar String Groups
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it ...