【一天一道LeetCode】#96. Unique Binary Search Trees

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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,找出1~n组成的二叉搜索树的个数!

可以参考【一天一道LeetCode】#95. Unique Binary Search Trees II

如果单单求个数的话,可以简化。

n=1时,num=1;n=2时,num=2;n=3时,num=5!

可以找出规律,num[i] = num[left]*num[right]!i从1取到n,就为num[n]的值。left和right为左右的节点个数。(节点个数小于等于1的时候记二叉搜索树个数为1)

以题目中的例子为例,取1为根节点,2,3组成的二叉搜索树个数为2(right),左边为1,所以1为根节点的二叉搜索树个数为2,依次可以算出,2为根节点时个数为1,3为根节点的个数为2,加起来为5!

class Solution {
public:
    int numTrees(int n) {
        vector<int> num(n+1,-1);//存放i个节点存放的二叉搜索树个数
        int ret = calNumTrees(1,n,num);
        return ret;
    }
    int calNumTrees(int start , int end , vector<int>& num)
    {
        if(num[end - start +1] != -1) return num[end - start +1];
        if(start >= end) return 1;//当少于等于1个节点时,二叉搜索树个数记为1
        int temp = 0;
        for(int i = start ; i <= end ; i++)//依次以1到n为根节点
        {
            int left = calNumTrees(start,i-1,num);//左边二叉搜索树的个数
            int right = calNumTrees(i+1,end,num);//右边二叉搜索树的个数
            temp +=(left*right);
        }
        if(num[end - start +1] == -1) num[end-start+1] = temp;//num[i]存放1~i组成的个数
        return temp;
    }
};
时间: 2024-10-10 09:30:52

【一天一道LeetCode】#96. Unique Binary Search Trees的相关文章

LeetCode 96 Unique Binary Search Trees不同的二叉搜索树的个数

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? 1 class Solution { 2 public: 3 int numTrees(int n) { 4 int *dp = new int[n+1]; 5 for(int i=0;i<n+1;++i) 6 dp[i] = 0; 7 8 dp[0] = 1;//!!! 9 for(int sz = 1; sz <

leetcode 96 Unique Binary Search Trees ----- java

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,然后将这n个数进行二叉排序树的排列,求有多少种组合. public class Sol

Java [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, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 解题思路: 动态规划法. 用G(n)表示长度为n组成的二叉搜索树的数目: G(0) = 1

leetCode 96.Unique Binary Search Trees (唯一二叉搜索树) 解题思路和方法

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 confused what "{1,#,2,3}

Leetcode #96 Unique Binary Search Trees

题目链接:https://leetcode.com/problems/unique-binary-search-trees/ 从最简单的情况开始考虑: n = 0 : 只有“空树”这一种情况.f[0] = 1. n = 1 : 只有“根节点”这一种情况.f[1] = 1. n = 2 : 由于“根节点”必须存在,所以只有一个节点的位置是可以变化的.即“左子树0个节点,右子树1个节点”,或者“左子树1个节点,右子树0个节点”.f[2] = f[0] * f[1] + f[1] * f[0]. 同理

[动态规划] leetcode 96 Unique Binary Search Trees

problem:https://leetcode.com/problems/unique-binary-search-trees/ 左子树个数 * 右子树个数 class Solution { public: int numTrees(int n) { vector<int> dp(n + 1); dp[1] = 1; for(int i = 2;i <= n;i++) { dp[i] = dp[i - 1] + dp[i - 1]; for(int j = 1;j <= i -

Leetcode 树 Unique Binary Search Trees

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Binary Search Trees Total Accepted: 13478 Total Submissions: 37858 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there

leetcode -day28 Unique Binary Search Trees I II

1.  Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 /

96. Unique Binary Search Trees &amp;&amp; 95. Unique Binary Search Trees II &amp;&amp; 241. Different Ways to Add Parentheses

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, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Tree Dynamic Program

Java for LeetCode 095 Unique Binary Search Trees II

Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 解题思路: 参考Java for LeetCode 096 Unique Binary Search Trees思路,本题很容易解决.注意,