96. Unique Binary Search Trees

原创 2018年04月17日 02:55:05

问题描述

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.

题目链接:


思路分析

给一数n,计算n个数可以生成多少个不同的二叉搜索树。

这是一个动态规划题目,对于n个数,在排序之后,如果选择第一个数作为根,那么就有G(0)种左子树和G(n-1)种右子树的排法;如果选第二个数为根,就有G(1)种左子树和G(n-2)种右子树的排法……G(0) = G(1) = 1,所以就可以用一个数组保存不同n的组成的BST的数目。每一个新的n重新计算即可。

代码

class Solution {
public:
    int numTrees(int n) {
        vector<int> G;
        G.push_back(1);
        for (int i = 1; i <= n; i++){
            int t = 0;
            for (int j = 0; j < i; j++)
                t += G[j] * G[i - j - 1];
            G.push_back(t);
        }
        return G.back();
    }
};

时间复杂度:O(n2)
空间复杂度:O(n)


反思

理解二叉搜索树的构造比较重要,它是可以选择任意一个数作为根的,只不过对于结构有特定的要求。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BigFatSheep/article/details/79969029

96 Unique Binary Search Trees

题目链接:https://leetcode.com/problems/unique-binary-search-trees/题目:Given n, how many structurally uniq...
  • ChilseaSai
  • ChilseaSai
  • 2015-11-25 21:04:50
  • 944

LeetCode96:Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ex...
  • u012501459
  • u012501459
  • 2015-06-24 15:56:10
  • 1852

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

题目:http://oj.leetcode.com/problems/unique-binary-search-trees/ Given n, how many structurally uniqu...
  • dragon_dream
  • dragon_dream
  • 2014-04-01 21:07:32
  • 2173

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

问题描述: Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For e...
  • zdavb
  • zdavb
  • 2015-08-10 15:14:02
  • 281

LeetCode 96:Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ex...
  • geekmanong
  • geekmanong
  • 2015-12-31 16:35:13
  • 2592

leetcode-java-96. Unique Binary Search Trees

思路:当有n个节点时(1--n),任何一个数都可以当成根节点,左边j个节点,右边是n-j-1个节点 所以就有nums[n] = nums[0]*nums[n]+nums[1]*nums[n-1]+nu...
  • github_34514750
  • github_34514750
  • 2016-06-30 10:08:42
  • 281

LeetCode || Unique Binary Search Trees

Unique Binary Search Trees  Total Accepted: 11525 Total Submissions: 32497My Submissions ...
  • jiadebin890724
  • jiadebin890724
  • 2014-04-09 23:16:41
  • 11223

leetcode96. Unique Binary Search Trees

题目链接:https://leetcode.com/problems/unique-binary-search-trees/ 题目: Given n, how many structurally ...
  • li563868273
  • li563868273
  • 2016-07-10 11:29:42
  • 296

Unique Binary Search Trees -- LeetCode

原题链接: http://oj.leetcode.com/problems/unique-binary-search-trees/  这道题要求可行的二叉查找树的数量,其实二叉查找树可以任意取根,只要...
  • linhuanmars
  • linhuanmars
  • 2014-04-30 06:16:29
  • 20904

95 Unique Binary Search Trees II

题目链接:https://leetcode.com/problems/unique-binary-search-trees-ii/题目:Given n, generate all structural...
  • ChilseaSai
  • ChilseaSai
  • 2015-11-28 12:59:53
  • 3380
收藏助手
不良信息举报
您举报文章:96. Unique Binary Search Trees
举报原因:
原因补充:

(最多只允许输入30个字)