Unique Binary Search Trees-LeetCode

本题是求有n个节点的二叉搜索树有多少种。二叉搜索树中的根节点和子节点要满足一定的性质,要先确定根节点,才能确定子节点。以i为根节点的二叉搜索树共有其左子树的种数乘以右子树的种数这么多种,因此,可以用一个循环穷举所有根节点的可能性,即根节点为0~n。然而,怎么知道当根节点为i时,其左右子树有多少种呢?我们采用自底向上方法即动态规划,计算节点数分别为0~n时二叉搜索树的种数。

class Solution{

public:
    int numTrees(int n)
    {
        vector<int> num(n + 1, 1);

        for (int i = 2; i <= n; ++i)
        {
            num[i] = 0;

            for (int j = 0; j < i; ++j)
            {
                num[i] += (num[j] * num[i-1-j]);
            }
        }

        return num[n];
    }
};

这是个两层循环,外层循环列举节点数为2~n时的情况(节点数为0和1的情况可以直接得到结果,作为迭代初始值),内层循环计算当节点数为i时,二叉搜索树有多少种:根节点为j+1,其左子树以j为根节点(若j 为0,则是棵空树),右子树以i-1-j为根节点,则以j为根节点的二叉搜索树之种数为其左子树的种数乘以右子树的种数;列举j 的值,分别计算以j+1为根节点的二叉搜索树的种数,对其求和,即得节点数为i时二叉搜索树的种数。

自底向上和动态规划的核心思想在于从简单的情况迭代计算出复杂的情况,当计算节点数为i的二叉搜索树的种数时,节点数小于i的二叉搜索树之种数已计算完成,可以直接使用,而自顶向下或递归方法会重复计算子问题。

时间: 2024-08-06 13:47:27

Unique Binary Search Trees-LeetCode的相关文章

Unique Binary Search Trees leetcode 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 题解:  这道题我自己是没啥思路的,感觉就是一种排列组合的计算,但是又不太会..然后网上查了

Unique Binary Search Trees——LeetCode

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,求它的

[LeetCode][JavaScript]Unique Binary Search Trees II

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 / / \ 2 1

[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 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 class Solution { private

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 /

[leetcode]Unique Binary Search Trees II @ Python

原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/ 题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考虑dfs了.dfs(start, end)函数返回以start,start+1,...,end为根的二叉查找树. 代码: # Definition for a binary tree node #

Unique Binary Search Trees II leetcode java

题目: 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 题解:这道题比1难的就是不是返回个数,而

LeetCode: Unique Binary Search Trees [095]

[题目] 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, 问用1,2,3,4,5...n这n个值,能构造多少棵合法的二叉

LeetCode: Unique Binary Search Trees II [096]

[题目] 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,#