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的二叉树共有h(n)种形态,其中h(n)是卡特兰数,h(n) = C(2*n,n)/(n+1);
所以只要按这个公式计算就是可以了。注意变量要取long long int型,否则会溢出。
代码:
 class Solution {
public:
int numTrees(int n) {
long long int n_factorial = ;
long long int tn_fac = ;
for(int i = ;i <= n;i ++)
n_factorial *= i;
for(int i = n+;i <= *n;i ++)
tn_fac *= i;
return (tn_fac)/(n_factorial*(n+));
}
};

题外话:为什么节点数为n的二叉树有h(n)种形态呢?

当n=0时,h(0)=1;

当n=1时,h(1)=1;

当n>=2时,分别考虑树的左子树和右子树,它们的节点数分别可以取(0,n-1),(1,n-2),....,(n-2,1),(n-1,0),所以h(n)=h(0)*h(n-1)+h(1)*h(n-2)+...+h(n-1,0)h(0),这个递推式解得到的数就是卡特兰数。

【leetcode刷题笔记】Unique Binary Search Trees的更多相关文章

  1. LeetCode(96) Unique Binary Search Trees

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

  2. LeetCode之“动态规划”:Unique Binary Search Trees && Unique Binary Search Trees II

    1. Unique Binary Search Trees 题目链接 题目要求: Given n, how many structurally unique BST's (binary search ...

  3. LeetCode(95) Unique Binary Search Trees II

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

  4. 【leetcode刷题笔记】Binary Tree Inorder Traversal

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

  5. 【leetcode刷题笔记】Binary Tree Preorder Traversal

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

  6. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  7. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. LeetCode(96)Unique Binary Search Trees

    题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...

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

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

随机推荐

  1. zabbix客户端安装shadowscoks客户端监控访问google网站

    配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加  Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本探测访问 ...

  2. mongodb在Windows安装配置及遇到的问题、java连接测试

    一.安装 1.访问mongodb的官网http://www.mongodb.org/downloads下载64bit的包,我下载的是mongodb-win32-x86_64-2008plus-ssl- ...

  3. ASP.NET CORE RAZOR :初始化数据库

    官方说法是:设定数据库种子https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/sql 应用背景:初次部署系统时,设定一 ...

  4. NodeJS示例异步式(Asynchronous)IO与同步式Synchronous)IO

    理解IO      IO(Input/Output)通常是指计算机线程进行慈磁盘读写或者网络通信时的一种行为.   同步式(Synchronous)IO和异步式(Asynchronous )IO   ...

  5. Java深入 - Java 内存分配和回收机制-转

    Java的GC机制是自动进行的,和c语言有些区别需要程序员自己保证内存的使用和回收. Java的内存分配和回收也主要在Java的堆上进行的,Java的堆中存储了大量的对象实例,所以Java的堆也叫GC ...

  6. ubuntu安装中文man手册

    1.安装manpages-zh包 sudo apt-get install manpages-zh 2.修改manpath文件 执行如下命令: vi /etc/manpath.config %s+/u ...

  7. 【C#学习笔记】之用button使得textbox中数字的值增减

    代码段: string t = ""; t = mv.textBox2.Text; int n = int.Parse(t); n = n + 1; mv.textBox2.Tex ...

  8. Downloading jQuery

    Compressed and uncompressed copies of jQuery files are available. The uncompressed file is best used ...

  9. [转]mysqlx 同时使用 AND OR

  10. php != 和 !== 的区别

    == and != do not take into account the data type of the variables you compare. So these would all re ...