LeetCode : Unique Binary Search Trees
2012-12-08 17:57
387 查看
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) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n == 0) return 1; if(n == 1) return 1; int total = 0; for(int i = 0; i< n; ++i){ int left = numTrees(i); int right = numTrees(n-i - 1); total += left * right; } return total; } };
相关文章推荐
- LeetCode——Unique Binary Search Trees II
- [LeetCode] Unique Binary Search Trees II (难以忍受的递归)
- 【leetcode刷题笔记】Unique Binary Search Trees
- LeetCode 之 Unique Binary Search Trees
- [LeetCode]Unique Binary Search Trees, 解题报告
- leetcode96~Unique Binary Search Trees
- [leetcode-95]Unique Binary Search Trees II(c++)
- [LeetCode][JavaScript]Unique Binary Search Trees II
- LeetCode | Unique Binary Search Trees
- Unique Binary Search Trees II - LeetCode
- [LeetCode] Unique Binary Search Trees
- [LeetCode] Unique Binary Search Trees
- LeetCode - Unique Binary Search Trees
- LeetCode - Unique Binary Search Trees II
- Unique Binary Search Trees (leetcode) 动态规划
- [Leetcode]Unique Binary Search Trees II
- leetcode || 96、Unique Binary Search Trees
- LeetCode Unique Binary Search Trees
- LeetCode No.95 Unique Binary Search Trees II
- LeetCode 57 Unique Binary Search Trees