【一天一道LeetCode】#96. Unique Binary Search Trees
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:给定一个数n,找出1~n组成的二叉搜索树的个数!
可以参考【一天一道LeetCode】#95. Unique Binary Search Trees II
如果单单求个数的话,可以简化。
n=1时,num=1;n=2时,num=2;n=3时,num=5!
可以找出规律,num[i] = num[left]*num[right]!i从1取到n,就为num[n]的值。left和right为左右的节点个数。(节点个数小于等于1的时候记二叉搜索树个数为1)
以题目中的例子为例,取1为根节点,2,3组成的二叉搜索树个数为2(right),左边为1,所以1为根节点的二叉搜索树个数为2,依次可以算出,2为根节点时个数为1,3为根节点的个数为2,加起来为5!
class Solution {
public:
int numTrees(int n) {
vector<int> num(n+1,-1);//存放i个节点存放的二叉搜索树个数
int ret = calNumTrees(1,n,num);
return ret;
}
int calNumTrees(int start , int end , vector<int>& num)
{
if(num[end - start +1] != -1) return num[end - start +1];
if(start >= end) return 1;//当少于等于1个节点时,二叉搜索树个数记为1
int temp = 0;
for(int i = start ; i <= end ; i++)//依次以1到n为根节点
{
int left = calNumTrees(start,i-1,num);//左边二叉搜索树的个数
int right = calNumTrees(i+1,end,num);//右边二叉搜索树的个数
temp +=(left*right);
}
if(num[end - start +1] == -1) num[end-start+1] = temp;//num[i]存放1~i组成的个数
return temp;
}
};
【一天一道LeetCode】#96. Unique Binary Search Trees的更多相关文章
- [LeetCode] 96. Unique Binary Search Trees 唯一二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 52. leetcode 96. Unique Binary Search Trees
96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- [LeetCode] 96. Unique Binary Search Trees(给定一个数字n,有多少个唯一二叉搜索树) ☆☆☆
[Leetcode] Unique binary search trees 唯一二叉搜索树 Unique Binary Search Trees leetcode java 描述 Given n, h ...
- [LeetCode] 96. Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Example ...
- leetcode 96 Unique Binary Search Trees ----- java
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Java [Leetcode 96]Unique Binary Search Trees
题目描述: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ...
- [leetcode]96. Unique Binary Search Trees给定节点形成不同BST的个数
Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? Input: ...
- [leetcode] 96 Unique Binary Search Trees (Medium)
原题 字母题 思路: 一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西. 1.求可行的二叉查找树的数量,只要满足中序遍历有序. 2.以一个结点为根的可行二叉树数量就是左右子树可行二叉树 ...
- [leetcode]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
随机推荐
- url重定向或者重写
有四种方式:1.urlMappings,返回200状态码 <system.web> <urlMappings > <add url="~/others.aspx ...
- Struts2 转换器
转换器 从一个 HTML 表单到一个 Action 对象,类型转换是从字符串到非字符串 Http 没有 "类型" 的概念,每一项表单的输入只可能是一个字符串或一个字符串数组,在服务 ...
- SpringMVC之拦截器实现登录验证
今天回头看之前发的javaweb学习路线图,发现把路线图中的也学的有一半多了,不过还是路漫漫.在前面的博客中有学习过spring的aop,它利用动态代理实现,在springmvc中也是一样,今天使用H ...
- Git之(五)远程管理
开篇就提到过,Git是一个分布式版本管理系统.但是到现在为止,我们所有的演练都是在本地Git仓库.如果想与他人合作,还需要一个远程的 Git 仓库.尽管技术上可以从个人的仓库里推送和拉取修改内容,但我 ...
- 20160215.CCPP体系详解(0025天)
程序片段(01):01.Malloc.c 内容概要:Malloc拓展 #include <stdio.h> #include <stdlib.h> //01.内存伸缩函数: / ...
- Most Common Solutions to FRM-41839 and .tmp Files Not Being Deleted
In this Document Symptoms Changes Cause Solution References APPLIES TO: Oracle Application ...
- boost::asio::spawn 将一统C++网络库
boost::asio::spawn 将一统C++网络库(金庆的专栏)boost::asio::spawn()创建一个协程,使C++网络编程大大简化,个人认为这使得 asio 成为C++首选网络库.b ...
- Redis中的关系查询
本文对Redis如何保存关系型数据,以及如何对其匹配.范围.模糊查询进行举例讲解,其中模糊查询功能基于最新的2.8.9以后版本. 1 关系型数据的存储 以Staff对象为例,在关系型数据库或类似Gri ...
- Linux 高性能服务器编程——IP协议详解
1 IP服务特点 IP协议是TCP/IP协议族的动力,它为上层协议提供无状态.无连接.不可靠的服务. 无状态:IP通信双方不同步传输数据的状态信息,因此IP数据包的发送.传输和接收都是无序的. ...
- 大数据基础知识问答----hadoop篇
handoop相关知识点 1.Hadoop是什么? Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速 ...