Leetcode - Tree /Dynamic Programming- Unique Binary Search
2014-08-14 15:05
531 查看
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.
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
class Solution { public: int numTrees(int n) { if(n==0) return 0; int *num=new int[n+1](); num[0]=1; num[1]=1; for(int i=2;i<=n;i++) { for(int j=1;j<=i;j++) num[i]+=num[j-1]*num[i-j]; } int temp=num ; delete []num; return temp; } };
相关文章推荐
- LeetCode – Refresh – Unique Binary Search Tree
- [LeetCode] Unique Binary Search Tree
- [LeetCode] Unique Binary Search Tree
- Unique Binary Search Tree | LeetCode
- LeetCode – Refresh – Unique Binary Search Tree II
- LeetCode题库解答与分析——#95. 不同的二叉查找树 IIUniqueBinarySearchTreeII
- [leetcode][tree] Unique Binary Search Trees II
- leetCode 96:Unique Binary Search Tree
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
- [Leetcode]Unique binary search tree
- leetcode最优解整理(dfs/String/Binary Search/Two pointers/Math/Dynamic programming)
- 【LeetCode】Unique Binary Search Tree
- leetcode unique binary search Tree
- Unique Binary Search Tree -- LeetCode
- LeetCode Week10: Edit Distance, Unique Binary Search Tree(I & II)
- 【Leetcode】Unique Binary Search Tree in JAVA
- LeetCode : Unique Binary Search Tree
- Unique Binary Search Tree - Leetcode
- LeetCode 95 Unique Binary Search Tree II(Python详解及实现)
- Unique Binary Search Tree II | LeetCode