leetCode(26):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
1)结构的总数和存储的n个有序数没有关系。即存放的是1,2,3或5、6、9结果都是一样的;
2)n=0\1时,结果都为1;n个有序数。取不论什么一个作为根结点,该值左边的为左子树。右边的为右子树,
在这样的情况下。可能的结构总数为:左子树结构数*右子树结构数。所以原问题就被分解成了两个子问题;
3)将取n个有序数作为根结点的情况的结构问题相加。就可以得到原问题的解,但应注意的是,如:1、2、3
取1或3作为根结点的情况,结构数是同样的,即具有对称性;那么也要考虑到奇偶性;
4)可以被分解成同样的子问题,自己就行採用递归,本程序採用循环的方法。
*/
int numTrees(int n)
{
	vector<int> store;
	store.push_back(1);
	store.push_back(1);
	for(int i=2;i<=n;++i)
	{
		int tmp=0;
		for(int j=0;j<i/2;++j)
		{
			tmp+=2*store[i-1-j]*store[j];
		}
		if(i%2)
			tmp+=store[i/2]*store[i/2];

		store.push_back(tmp);
	}
	return store[n];
}
时间: 2024-08-09 06:32:38

leetCode(26):Unique Binary Search Trees的相关文章

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

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 /

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

【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

[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

题目链接:https://leetcode.com/problems/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.