Leetcode 树 Unique Binary Search Trees

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Unique Binary Search Trees

Total Accepted: 13478 Total
Submissions: 37858

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,问以1至n的值为节点的二叉查找树有多少棵

思路:

先看一个例子。当n=3时,有5棵二叉查找树,分别为

1             1                2             3            3

\             \              / \           /            /

2  3            1   3         2            1

\ /                          /          \

3         2                          1                2

以i为根的二叉查找树,其左子树是由[1,i-1]构成,右子树是由[i+1,n]构成。

总的二叉查找树的数量等于左子树的数量乘以右子树的数量

例如以1为根的二叉查找树的数量为2,等于它的左子树的数量1(一棵空子树)乘以

它的右子树的数量2.

实现的时候可以用递归实现

递归函数:

int numTrees(int begin, int end)

表示以[begin, end]中的值为节点的二叉查找树的数量

优化:

加入记忆化搜索。由于二叉查找树的数目只跟有多少个数有关,而跟这此数是在哪个区间的无关,

所以可以用f[i]表示以i个数为节点的二叉查找数的棵数。

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

思路2:前面的分析和思路1一样,后面采用动态规划

用f[i]表示以i个数为节点的二叉查找数的棵数

状态转移方程为:

f[i] = sum_k (f[k - 1] * f[i - k])

复杂度:时间O(n^2),空间O(n)

注:二叉查找树(来自wikipedia):也称有序二叉树,排序二叉树,是指一棵空树

具有下列性质的二叉树:

1.若任意节点的左子树不空,左子树上的所有结点的值均小于它的根结点的值

2.若任意节点的右子树不空,右子树上的所有结点的值均大于它的根结点的值

3.任意节点的左、右子树也分别为二叉查找树

4.没有键值相等的节点

相关题目:Unique Binary Search Trees II

//思路1
class Solution {
public:
    int numTrees(int n){
		f = vector<int>(n + 1, 0);
    	return numTrees(1, n);
    }

    int numTrees(int begin, int end){
		int n = end - begin + 1;
		if(f[n]) return (f[n]);
    	if(begin > end) return 1;
    	int sum = 0;
    	for(int i = begin; i <= end; i++){
    		sum += numTrees(begin, i - 1) * numTrees(i + 1, end);
    	}
		f[n] = sum;
    	return sum;
    }
private:
	vector<int> f;
};
//思路2
class Solution {
public:
	int numTrees(int n){
		vector<int> f(n + 1, 0);
		f[0] = 1;

		for(int i = 1; i <= n; i ++){
			for(int k = 1; k <= i; k ++){
				f[i] += f[k - 1] * f[i - k];
			}
		}
		return f[n];
	}
};

Leetcode 树 Unique Binary Search Trees,布布扣,bubuko.com

时间: 2024-08-24 21:48:18

Leetcode 树 Unique Binary Search Trees的相关文章

leetcode -day28 Unique Binary Search Trees I II

1.  Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 /

Leetcode 树 Unique Binary Search TreesII

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Binary Search Trees II Total Accepted: 7349 Total Submissions: 27648 Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3,

Java for LeetCode 095 Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 解题思路: 参考Java for LeetCode 096 Unique Binary Search Trees思路,本题很容易解决.注意,

[LeetCode][JavaScript]Unique Binary Search Trees II

Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1

[C++]LeetCode: 92 Unique Binary Search Trees II

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,

[LeetCode][Java]Unique Binary Search Trees II

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题意: 给定n ,生成所有的存在且唯一的

【LeetCode】Unique Binary Search Trees II 不同的二叉查找树II

今早起来做 LeetCode,结果被这道题卡了将近1个半小时,忍着没有去搜答案,最后还是被我想出来了,而且直接一次AC,哈哈!现在在这里记录一下解题思路. 原题: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's sh

【LeetCode】Unique Binary Search Trees (2 solutions)

Unique Binary Search Trees 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 Base case: n==0, n==1时,f

leetcode:Unique Binary Search Trees

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 注意:二分查找树的定义是,左子树节点均小于root,右子树节点均大于root!不要想当然地将某个点作为