Java [Leetcode 96]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

解题思路:

动态规划法。

用G(n)表示长度为n组成的二叉搜索树的数目;

G(0) = 1, G(1) = 1

F(i,n)表示以i为根节点,长度为n组成的二叉搜索树的数目。

从而G(n) = F(1,n) + F(2,n) + ...+ F(n,n)

而F(i,n) = G(i - 1) * G(n - i)  1<=i <=n

从而G(n) = G(0) * G(n - 1) + G(1) * G(n - 2) + ... + G(n - 1) * G(0)

代码如下:

public class Solution{
	public int numTrees(int n){
		int[] res = new int[n + 1];
		res[0] = res[1] = 1;

		for(int i = 2; i <= n; i++){
			for(int j = 0; j <= i - 1; j++){
				res[i] += res[j] * res[i - 1 - j];
			}
		}

		return res[n];
	}
}

  

时间: 2024-10-08 03:54:13

Java [Leetcode 96]Unique Binary Search Trees的相关文章

leetcode 96 Unique Binary Search Trees ----- java

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,然后将这n个数进行二叉排序树的排列,求有多少种组合. public class Sol

LeetCode 96 Unique Binary Search Trees不同的二叉搜索树的个数

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 1 class Solution { 2 public: 3 int numTrees(int n) { 4 int *dp = new int[n+1]; 5 for(int i=0;i<n+1;++i) 6 dp[i] = 0; 7 8 dp[0] = 1;//!!! 9 for(int sz = 1; sz <

leetCode 96.Unique Binary Search Trees (唯一二叉搜索树) 解题思路和方法

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,#,2,3}

Leetcode #96 Unique Binary Search Trees

题目链接:https://leetcode.com/problems/unique-binary-search-trees/ 从最简单的情况开始考虑: n = 0 : 只有“空树”这一种情况.f[0] = 1. n = 1 : 只有“根节点”这一种情况.f[1] = 1. n = 2 : 由于“根节点”必须存在,所以只有一个节点的位置是可以变化的.即“左子树0个节点,右子树1个节点”,或者“左子树1个节点,右子树0个节点”.f[2] = f[0] * f[1] + f[1] * f[0]. 同理

[动态规划] leetcode 96 Unique Binary Search Trees

problem:https://leetcode.com/problems/unique-binary-search-trees/ 左子树个数 * 右子树个数 class Solution { public: int numTrees(int n) { vector<int> dp(n + 1); dp[1] = 1; for(int i = 2;i <= n;i++) { dp[i] = dp[i - 1] + dp[i - 1]; for(int j = 1;j <= i -

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 树 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 /

96. Unique Binary Search Trees &amp;&amp; 95. Unique Binary Search Trees II &amp;&amp; 241. Different Ways to Add Parentheses

96. 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 Tree Dynamic Program