[leetcode] 96 Unique Binary Search Trees (Medium)
字母题
思路:
一开始妹有一点思路,去查了二叉查找树,发现有个叫做卡特兰数的东西。
1、求可行的二叉查找树的数量,只要满足中序遍历有序。
2、以一个结点为根的可行二叉树数量就是左右子树可行二叉树数量的乘积。
3、总的数量是将以所有结点为根的可行结果累加起来。
n = 0 时,因为空树也算一种二叉搜索树,则dp[0]=1;
n = 1时,dp[1]=1;
n = 2时
dp[2] = dp[0] * dp[1] (1为根的情况)
+ dp[1] * dp[0] (2为根的情况)
n = 3时
dp[3] = dp[0] * dp[2] (1为根的情况)
+ dp[1] * dp[1] (2为根的情况)
+ dp[2] * dp[0] (3为根的情况)
写成表达式如下:
class Solution
{
public:
int numTrees(int n)
{
if (n <= 0)
return 0;
int res[n + 1] = {0};
res[0] = 1;
res[1] = 1;
for (int i = 2; i <= n; i++)
{
for (int j = 0; j < i; j++)
{
res[i] += res[i - j - 1] * res[j];
}
}
return res[n];
}
};
[leetcode] 96 Unique Binary Search Trees (Medium)的更多相关文章
- [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]95. Unique Binary Search Trees II给定节点形成不同BST的集合
Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...
随机推荐
- c# Lambda扩展
扩展类 public static class LinqExtensions { /// <summary> /// 创建lambda表达式:p=>true /// </sum ...
- ASP.NET Web API Controller 是怎么建成的
先看ASP.NET Web API 讯息管线: 註:为了避免图片太大以至于超过版面,上图中的「HTTP 讯息处理程序」区块省略了 HttpRoutingDispatcher 处理路由分派的部分.「控制 ...
- hgoi#20190516
T1-Buying A House 给你一个长度为n的序列a,给你目标房子m,最多花的钱k 如果a[i]为0,这座房子无法购买,否则可以购买,求能买的距离目标房子最近的房子,输出最小距离 两座房子之间 ...
- Unity 通用透明物体漫反射Shader(双面渲染&多光源&光照衰减&法线贴图&凹凸透明度控制)
Shader "MyUnlit/AlphaBlendDiffuse" { Properties { _Color("Color Tint(贴图染色)",Colo ...
- SpringBoot中资源初始化加载的几种方式(看这一片就够了)
一.问题 在平时的业务模块开发过程中,难免会需要做一些全局的任务.缓存.线程等等的初始化工作,那么如何解决这个问题呢?方法有多种,但具体又要怎么选择呢? 二.资源初始化 1.既然要做资源的初始化,那么 ...
- 使用wireshark捕获SSL/TLS包并分析
原创博客,转载请注出处! TLS运作方式如下图:
- rm、shutdown、磁盘挂载、vi使用方法
1. 系统管理文件 1.1 rm 文件与目录有关命令 删除命令 (慎用) --- 数据是否备份了 rm === remove rm /oldboy/oldboy.txt --- 删除文件 rm ...
- 【实战】SpringBoot + KafKa
1.配置pom包 <dependency> <groupId>org.springframework.kafka</groupId> <artifactId& ...
- C语言-main方法的两个参数是干什么的?
大家都知道C语言的main方法怎么写的吧!但你们知道mian方法里的参数的含义吗? 代码如下: int main(int argc,char *argv[]){ //argc是传进的参数个数 //ar ...
- JS工具整理
1.获取今日日期:摘抄地址:https://www.cnblogs.com/carekee/articles/1678041.html getTodayFmt('yyyy-MM-dd') getTod ...