关闭

96. Unique Binary Search Trees

163人阅读 评论(0) 收藏 举报
分类:
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

寻找总结点数为n的构建二叉树的可能数目。这种题肯定是递归或者动态规划,明显的子问题拆解。比如例子中的5, 你把1放在root,剩下的只能放在右侧,变为规模为4的问题,如果把3放在中间,俩在左,三个在右面,变成了规模2的结果乘上规模为3的结果。
所以可以构建dp[n+1],其中dp[0]=1, dp[1]=1。递归公式为:
catalan
这个公式也就是卡塔兰数的递推表达,具体见链接里的介绍。总之这种递推解决方法还是比较好想到的,具体代码如下:
时间复杂发O(n^2),空间复杂度O(n).

int numTrees(int n) {
    int dp[n+1] = {0};
    if (n == 0) return 0;
    dp[0] = 1;
    dp[1] = 1;
    for (int i = 2; i < n+1; i++) {
        for (int j = 0; j < i; j++) {
            dp[i] += dp[j] * dp[i-j-1];
        }
    }
    return dp[n];
}
1
0
查看评论

LeetCode96: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, ...
  • u012501459
  • u012501459
  • 2015-06-24 15:56
  • 1629

96 Unique Binary Search Trees

题目链接:https://leetcode.com/problems/unique-binary-search-trees/题目:Given n, how many structurally unique BST's (binary search trees) that store valu...
  • ChilseaSai
  • ChilseaSai
  • 2015-11-25 21:04
  • 888

LeetCode 96: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, ...
  • geekmanong
  • geekmanong
  • 2015-12-31 16:35
  • 2539

[C++]LeetCode: 53 Unique Binary Search Trees

题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n&#...
  • cinderella_niu
  • cinderella_niu
  • 2014-12-23 20:09
  • 852

95 Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/题目:Given n, generate all structurally unique BST's (binary search trees) that sto...
  • ChilseaSai
  • ChilseaSai
  • 2015-11-28 12:59
  • 2924

LeetCode 96 — Unique Binary Search Trees(C++ Java Python)

题目:http://oj.leetcode.com/problems/unique-binary-search-trees/ Given n, how many structurally unique BST's (binary search trees) t...
  • dragon_dream
  • dragon_dream
  • 2014-04-01 21:07
  • 2131

[leetcode-96]Unique Binary Search Trees(c++)

问题描述: 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 uniqu...
  • zdavb
  • zdavb
  • 2015-08-10 15:14
  • 239

Unique Binary Search Trees -- LeetCode

原题链接: http://oj.leetcode.com/problems/unique-binary-search-trees/  这道题要求可行的二叉查找树的数量,其实二叉查找树可以任意取根,只要满足中序遍历有序的要求就可以。从处理子问题的角度来看,选取一个结点为根,就把结点...
  • linhuanmars
  • linhuanmars
  • 2014-04-30 06:16
  • 20324

LeetCode | Unique Binary Search Trees II

题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given ...
  • lanxu_yy
  • lanxu_yy
  • 2013-12-23 14:58
  • 9720

leetcode 95. Unique Binary Search Trees II | Java最短代码实现

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3,...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-03-09 20:43
  • 689
    个人资料
    • 访问:26552次
    • 积分:1781
    • 等级:
    • 排名:千里之外
    • 原创:148篇
    • 转载:4篇
    • 译文:1篇
    • 评论:2条
    文章分类
    最新评论