Unique Binary Search Trees
2015-05-02 02:29
148 查看
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 buf[n+1]; buf[0] = 1; buf[1] = 1; for (int i = 2; i <= n; i++) { int result = 0; for (int j = 0; j < i; j++) { result += buf[j] * buf[i-1-j]; } buf[i] = result; } return buf ; } };
相关文章推荐
- LeetCode - Unique Binary Search Trees
- 【Leetcode长征系列】Unique Binary Search Trees
- Leetcode 95 Unique Binary Search Trees II
- 96. Unique Binary Search Trees
- LeetCode Unique Binary Search Trees--动态规划思想
- Leetcode-95. Unique Binary Search Trees II
- LeetCode96_Unique Binary Search Trees(求1到n这些节点能够组成多少种不同的二叉查找树) Java题解
- Unique Binary Search Trees
- 95. Unique Binary Search Trees II
- LeetCode(95) Unique Binary Search Trees II
- LeetCode OJ Unique Binary Search Trees
- leetcode_Unique Binary Search Trees_easy
- Unique Binary Search Trees II
- leetcode Unique Binary search Trees
- LeetCode--unique-binary-search-trees
- Leetcode 96 Unique Binary Search Trees(独一的二叉搜索树)
- leetcode_96_Unique Binary Search Trees
- different/Unique/distinct Binary Search Trees
- LeetCode | Unique Binary Search Trees II(构建二叉搜索树)
- 96. Unique Binary Search Trees