[LeetCode] Unique Binary Search Tree
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个结点的二叉查找树有b[n]个,则设想对一个排好序的list,我们从第一个元素开始枚举根节点。对于每一个根节点,这棵二叉查找树的可能是b[left] * b[right], left < n, right < n。
故我们只需要取b[0] = 0, b[1] = 1, 然后迭代计算b[i]即可。
class Solution {
public:
int numTrees(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(n == )
return ; int *num;
num = new int[n + ];
num[] = ;
num[] = ;
for(int i = ;i <= n;i++)
{
num[i] = ;
for(int j = ;j < i;j++)
num[i] += num[j] * num[i - - j];
} return num[n];
}
};
[LeetCode] Unique Binary Search Tree的更多相关文章
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- Unique Binary Search Tree II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析
本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- LeetCode Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [LeetCode] Validate Binary Search Tree 验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- 10Oracle Database 数据表数据查询
Oracle Database 数据表数据查询 DML 数据操纵语言 - 数据的查看和维护 select / insert /delete /update 基本查询语句 Select [distinc ...
- python黑科技库:FuckIt.py,让你代码从此远离bug
今天给你推荐的这个库叫 “FuckIt.py”,名字一看就是很黄很暴力的那种,作者是这样介绍它的: FuckIt.py uses state-of-the-art technology to make ...
- Cmake的介绍和使用 Cmake实践
Cmake的介绍和使用 Cmake实践http://www.cppblog.com/Roger/archive/2011/11/17/160368.html
- RequestMapping_PathVariable注解
[@PathVariable 映射URL绑定的占位符] 1.带占位符的URL是Spring 3.0 新增的功能,该功能在Spring MVC向 REST 目标挺进发展过程中具有里程碑的意义. 2.通过 ...
- ansible plugins简介
ansible插件是增强ansible的核心功能的代码片段,ansible使用插件架构来实现丰富,灵活和可扩展的功能集. Ansible提供了许多方便的插件,您可以轻松编写自己的插件. 下边简单介绍A ...
- noip模拟赛 动态规划
题目描述LYK在学习dp,有一天它看到了一道关于dp的题目.这个题目是这个样子的:一开始有n个数,一段区间的价值为这段区间相同的数的对数.我们想把这n个数切成恰好k段区间.之后这n个数的价值为这k段区 ...
- noip模拟赛 寻宝之后
题目背景 还记得NOIP2011的寻宝吗?6年之后,小明带着他的妹子小芳,再次走上了寻宝的道路. 然而这次他们寻宝回来之后,小明被困在了一个迷宫中. 题目描述 迷宫是一个n*m的字符矩阵. 小明在这个 ...
- [K/3Cloud] K/3 Cloud1.0怎样和2.0共存在一台服务器上
第一步:安装Cloud1.0,创建管理中心,创建业务数据中心,备份管理数据库和业务数据库,并且备份安装目录: 第二步:卸载Cloud1.0,清理安装目录,安装Cloud2.0,创建管理中心,创建业务数 ...
- CF899F. Letters Removing
给一个字符串支持以下操作:区间删除某个特定字符.最后输出字符串.n,m<=200000. 这题我居然不会可以回家了.. 首先,单点删除,选个平衡树比如set. 然后,他给的下标是会随删除操作变化 ...
- jQuery动态添加表格1
用jquery的append方法在指定行的后面新增一行tr,会把新增的行的html追加到指定行的html里面 content +="<tr><td>123</t ...