LeetCode题解:Unique Binary Search Trees
来源:互联网 发布:阿里云6元虚拟主机 编辑:程序博客网 时间:2024/06/06 23:54
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[n]; }};
- LeetCode题解:Unique Binary Search Trees
- [题解][Leetcode][Unique Binary Search Trees]
- 【LeetCode】【Python题解】Unique Binary Search Trees
- LeetCode题解:Unique Binary Search Trees
- LeetCode题解:Unique Binary Search Trees
- LeetCode题解:Unique Binary Search Trees II
- LeetCode 96. Unique Binary Search Trees 题解
- Unique Binary Search Trees题解
- LeetCode 题解(27): Unique Binary Search Trees
- LeetCode 题解(156): Unique Binary Search Trees II
- LeetCode题解——Unique Binary Search Trees
- LeetCode题解——Unique Binary Search Trees II
- LeetCode 96. Unique Binary Search Trees 题解 —— Java
- LeetCode: Unique Binary Search Trees
- LeetCode : Unique Binary Search Trees
- [Leetcode] Unique Binary Search Trees
- [LeetCode]Unique Binary Search Trees
- LeetCode Unique Binary Search Trees
- oninput,onpropertychange,onchange
- Ruby on Rails与Java,哪一个才适合你呢?
- HTTP详解
- ADSL再次断线,修改配置加启用脚本来监测了
- Populating Next Right Pointers in Each Node 完美二叉树添加next指针 @LeetCode
- LeetCode题解:Unique Binary Search Trees
- Single Number 数组找只出现一次的数@LeetCode
- Linked List Cycle 有环的链表@LeetCode
- Unique Binary Search Trees 求BST的组合总数 @LeetCode
- Web & Java: web server vs. app server
- Binary Tree Preorder Traversal 二叉树的先序@LeetCode
- LeetCode题解:Maximum Subarray
- Adding and Removing Tags on GitHub
- Maximum Subarray 最大子数组和(连续)@LeetCode