LeetCode题解——Unique Binary Search Trees
2015-11-07 20:47
288 查看
题目:
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.
解题思路:
The problem can be solved in a dynamic programming way. I’ll explain the intuition and formulas in the following.
Given a sequence 1…n, to construct a Binary Search Tree (BST) out of the sequence, we could enumerate each number i in the sequence, and use the number as the root, naturally, the subsequence 1…(i-1) on its left side would lay on the left branch of the root,
and similarly the right subsequence (i+1)…n lay on the right branch of the root. We then can construct the subtree from the subsequence recursively. Through the above approach, we could ensure that the BST that we construct are all unique, since they have
unique roots.
The problem is to calculate the number of unique BST. To do so, we need to define two functions:
BST, and the sequence ranges from 1 to n.
As one can see,
the problem. And
First of all, given the above definitions, we can see that the total number of unique BST
is the sum of BST
Particularly, the bottom cases, there is only one combination to construct a BST out of a sequence of length 1 (only a root) or 0 (empty tree). i.e.
Given a sequence 1…n, we pick a number i out of the sequence as the root, then the number of unique BST with the specified root
is the cartesian product of the number of BST for its left and right subtrees. For example,
BST out of the right subsequence [4, 5, 6, 7], and then combine them together (i.e.cartesian product). The tricky part is that we could consider the number of unique BST out of sequence [1,2] as
and the number of of unique BST out of sequence [4, 5, 6, 7] as
i.e.
Combining the above two formulas, we obtain the recursive formula for
In terms of calculation, we need to start with the lower number, since the value of
on the values of
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
解题思路:
The problem can be solved in a dynamic programming way. I’ll explain the intuition and formulas in the following.
Given a sequence 1…n, to construct a Binary Search Tree (BST) out of the sequence, we could enumerate each number i in the sequence, and use the number as the root, naturally, the subsequence 1…(i-1) on its left side would lay on the left branch of the root,
and similarly the right subsequence (i+1)…n lay on the right branch of the root. We then can construct the subtree from the subsequence recursively. Through the above approach, we could ensure that the BST that we construct are all unique, since they have
unique roots.
The problem is to calculate the number of unique BST. To do so, we need to define two functions:
G(n): the number of unique BST for a sequence of length n.
F(i, n), 1 <= i <= n: the number of unique BST, where the number i is the root of
BST, and the sequence ranges from 1 to n.
As one can see,
G(n)is the actual function we need to calculate in order to solve
the problem. And
G(n)can be derived from
F(i, n), which at the end, would recursively refer to
G(n).
First of all, given the above definitions, we can see that the total number of unique BST
G(n),
is the sum of BST
F(i)using each number i as a root. i.e.
G(n) = F(1, n) + F(2, n) + ... + F(n, n).
Particularly, the bottom cases, there is only one combination to construct a BST out of a sequence of length 1 (only a root) or 0 (empty tree). i.e.
G(0)=1, G(1)=1.
Given a sequence 1…n, we pick a number i out of the sequence as the root, then the number of unique BST with the specified root
F(i),
is the cartesian product of the number of BST for its left and right subtrees. For example,
F(3, 7): the number of unique BST tree with number 3 as its root. To construct an unique BST out of the entire sequence [1, 2, 3, 4, 5, 6, 7] with 3 as the root, which is to say, we need to construct an unique BST out of its left subsequence [1, 2] and another
BST out of the right subsequence [4, 5, 6, 7], and then combine them together (i.e.cartesian product). The tricky part is that we could consider the number of unique BST out of sequence [1,2] as
G(2),
and the number of of unique BST out of sequence [4, 5, 6, 7] as
G(4). Therefore,
F(3,7) = G(2) * G(4).
i.e.
F(i, n) = G(i-1) * G(n-i) 1 <= i <= n
Combining the above two formulas, we obtain the recursive formula for
G(n). i.e.
G(n) = G(0) * G(n-1) + G(1) * G(n-2) + … + G(n-1) * G(0)
In terms of calculation, we need to start with the lower number, since the value of
G(n)depends
on the values of
G(0) … G(n-1).
class Solution { public: int numTrees(int n) { //G(n) = F(1,n)+F(2,n)+F(3,n)+...+F(n,n) the total number of BST of sequence 1,2,...,n //F(i,n) = G(i-1)*G(n-i) the root is i,and the left BST is composed of 1,2,..,i-1; the right BST is composed of i+1,...,n //So,G(n) = G(0)*G(n-1)+G(1)*G(n-2)+G(2)*G(n-3)+...+G(i-1)*G(n-i)+...+G(n-1)*G(0); //if n%2 == 0; G(n)= (G(0)*G(n-1)+G(1)*G(n-2)+...+G(n/2-1)G(n/2))*2 //if n%2!=0; G(n) = (G(0)*G(n-1)+G(1)*G(n-2)+...+G(n/2-1)G(n/2+1))*2+G(n/2+1)*G(n/2+1) int *G = new int[n+1]; G[0] = G[1]=1; for(int i=2;i<=n;i++){ G[i] = 0; if(i%2==0){ for(int j=0;j<i/2;j++){ G[i]+=G[j]*G[i-j-1]; } G[i]*=2; } else{ for(int j=0;j<i/2;j++){ G[i]+=G[j]*G[i-j-1]; } G[i]*=2; G[i]+=G[i/2]*G[i/2]; } } return G ; } };
相关文章推荐
- iOS讲解迷惑--生成uuid
- Integer.parseInt()和Integer.valueOf()有什么区别
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- Sublime Text 3 (Build 3059) 证书、汉化、环境配置、常用快键
- iOS7中计算UILabel中字符串的高度
- Modern UI for WPF笔记 创建空白窗口
- [Android UI] 波浪特效的实现
- UI设计师不可不知的安卓屏幕知识
- LeetCode -- Repeated DNA Sequences
- 使用mysql_query()方法操纵数据库以及综合实例
- 工作队列(workqueue) create_workqueue/schedule_work/queue_work
- Qt Quick 事件处理之信号与槽
- 在android的spinner中,实现取VALUE值和ID值
- IOS 自定义 UIView 实现重用
- iOS开发25-iOS可视化编程-Interface Builder
- UVA 11235 Frequent values
- windows SysinternalsSuite
- 设置UILabel可变高度(根据文本内容自动适应高度)
- client sent invalid header line: "X-Hts_user: true" while reading client request headers
- UIAlertController的使用