96. Unique Binary Search Trees

My Submissions

Question
Editorial Solution
Total Accepted: 82788 Total
Submissions: 220611 Difficulty: Medium

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

思路:  1 2 3 4 5 | 6 |  7 ..........n

既然是二叉搜索树,先递增排好序,然后在序列中选定一个根,比如上图的6

然后分为左子树和右子树,这样便可以转化为子问题

设f(n)为n个数的二叉排序树的数目

则f(n) = f(0)f(n-1)+f(1)f(n-2)+.......+f(n-1)f(0)

自底向上写出公式即可

时间复杂度O(n*n),空间O(1)

效率更高可考虑直接用卡特兰公式

class Solution {
public:
int numTrees(int n) {
vector<int> f(n+1);
f[0]=1;
for(int i=1;i<=n;++i)
{
for(int k=0;k<=i-1;++k)
f[i]+=f[k]*f[i-1-k];
}
return f[n];
}
};

64-Unique Binary Search Trees的更多相关文章

  1. 52. leetcode 96. Unique Binary Search Trees

    96. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) tha ...

  2. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  4. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  5. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  6. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  7. 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 ...

  8. LeetCode: Unique Binary Search Trees II 解题报告

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  9. LeetCode - Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  10. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

随机推荐

  1. Git浅析

    Git浅析 索引 Git的常用命令 GitHub的使用 Git版本创建和回退 Git的工作区和暂存区 Git分支管理 1-Git的常用命令 01.创建一个版本库--进入相应的目录 git init 可 ...

  2. win10安装git fatal: open /dev/null or dup failed: No such file or directory错误解决方法

    原因看大家意思应该是 非即插即用驱动文件null.sys问题. 网上有很多方案.最后试了一个可行的. 替换  windows/system32/drivers/null.sys为网盘中的文件即可. 链 ...

  3. [对对子队]会议记录5.16(Scrum Meeting3)

    今天已完成的工作 何瑞 ​ 工作内容:搭建关卡5.6,优化之前的成本系统 ​ 相关issue:搭建关卡4.5.6 ​ 相关签入:feat: 第五第六关搭建完成 吴昭邦 ​ 工作内容:搭建关卡5.6 ​ ...

  4. springboot读取配置文件中的信息

    在一个项目中,我们有时候会把一些配置信息写入到一个配置文件中,在java代码中读取配置文件的信息.在此记录下读取属性文件中的内容. 在springboot项目中,springboot的配置文件可以使用 ...

  5. RabbitMQ处理未被路由的消息

    我们经常使用消息队列进行系统之间的解耦,日志记录等等.但是有时候我们在使用 RabbitMQ时,由于exchange.bindKey.routingKey没有设置正确,导致我们发送给交换器(excha ...

  6. DP接口中AUX

    背景技术: DP接口(DisplayPort)是一种图像显示接口,它不仅可以支持全高清显示分辨率(1920×1080),还能支持4k分辨率(3840×2160),以及最新的8k分辨率(7680×432 ...

  7. 小白自制Linux开发板 十. NES游戏玩起来

    本篇基于我们制作的Debian文件系统而展开,而且我们这会玩一些高级的操作方式--用我们的小电脑进行程序编译.   所以本篇操作全部都在我们个的开发板上完成.   1. 开发环境搭建 首先安装gcc, ...

  8. binary-tree-preorder-traversal leetcode C++

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

  9. WSL2学习和使用汇集

    接触WSL2过程中整理沉淀的一些知识点,大纲如下,内容比较多,详细内容参考https://www.yuque.com/wushifengcn/kb/mbg1b5 欢迎感兴趣者补充和提出问题,共同学习. ...

  10. Vulnstack内网靶场4

    环境 漏洞详情 (qiyuanxuetang.net) 仅主机模式内网网段192.168.183.0/24 外网网段192.168.157.0/24 其中Ubuntu作为对外的内网机器 攻击机kali ...