LeetCode题解:Unique Binary Search Trees
2014-11-06 16:39
330 查看
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
思路:
典型的动态规划题目。
BST(n) = Sum(i, 1, n, BST(i - 1) * BST(n - i))
其中(i, 1, n)表示i的取值范围从1到n。
题解:
class Solution { public: vector<int> history; Solution() { history.push_back(1); history.push_back(1); history.push_back(2); } int numTrees(int n) { while(history.size() <= n) { int last = history.size(); int accumu = 0; for(int i = 1 ;i <= last; ++i) accumu += history[i - 1] * history[last - i]; history.push_back(accumu); } return history ; } };
相关文章推荐
- [题解][Leetcode][Unique Binary Search Trees]
- 【题解】【BST】【Leetcode】Unique Binary Search Trees
- LeetCode题解——Unique Binary Search Trees
- LeetCode题解:Unique Binary Search Trees
- LeetCode题解:Unique Binary Search Trees II
- [LeetCode]题解(python):095-Unique Binary Search Trees II
- [LeetCode]题解(python):096-Unique Binary Search Trees
- LeetCode题解——Unique Binary Search Trees II
- 【LeetCode】【Python题解】Unique Binary Search Trees
- LeetCode题解:Unique Binary Search Trees
- leetcode-96-Unique Binary Search Trees
- leetcode Unique Binary Search Trees II
- LeetCode_DP_Unique Binary Search Trees
- Leetcode之unique-binary-search-trees
- leetcode Unique Binary Search Trees II java
- [LeetCode] Unique Binary Search Trees, Solution
- 【LeetCode】95.Unique Binary Search Trees II(Medium)解题报告
- LeetCode - Unique Binary Search Trees II
- leetcode 95&96. Unique Binary Search Trees
- [LeetCode] Unique Binary Search Trees II