Java for LeetCode 096 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.

解题思路:

二叉查找树,刚上来无从下手,但仔细想想就能发现规律:以i为根节点,i的左子树都是小于i的,共有numTrees(i-1)种情况,i的右子树都是大于i的,共有numTrees(n-i) 种情况,相乘即是以i为根节点的情况,然后依次循环即可,递归下即可解决。当然更高效的方式是采用DP来替代递归,JAVA实现如下:

    public int numTrees(int n) {
        if (n <= 0)
			return 0;
		int[] dp = new int[n + 1];
		dp[0] = 1;
		for (int i = 1; i < dp.length; i++)
			for (int k = 0; k <= i - 1; k++)
				dp[i] += dp[k] * dp[i - k - 1];
		return dp[n];
    }
时间: 2024-08-28 13:25:57

Java for LeetCode 096 Unique Binary Search Trees的相关文章

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 /

096 Unique Binary Search Trees

题目: 096 Unique Binary Search Trees 这道题目就是catalan数, 因为递归关系 为       代码为 class Solution: # @param {integer} n # @return {integer} def numTrees(self, n): # Catalan Number ans = 1 for i in range(1, n+1): ans *= (n+i)*1.0/i return int(round(ans/(n+1)))

[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 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