LeetCode_Unique Binary Search Trees II

一.题目

Unique Binary Search Trees II

Total Accepted: 32757 Total Submissions: 117071My
Submissions

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}" means? >
read more on how binary tree is serialized on OJ.

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

二.解题技巧

这道题和Unique Binary Search Trees很相似,只是这道题可能无法使用动态规划减少计算的时间,因为每一个节点的值是不一样的,不存在相同的子问题的情况,因此,计算复杂度无法下降。使用递归的方式进行的时候,时间复杂度为O(n^3),空间复杂度为O(n)。

三.实现代码

#include <iostream>
#include <vector>
#include <unordered_map>

using std::vector;
using std::unordered_map;

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution
{
private:
    unordered_map<int, vector<TreeNode*> > PreResult;

    vector<TreeNode*> generateTrees(int n, int base)
    {
        vector<TreeNode*> Result;

        if (n == 0)
        {
            TreeNode* TmpNode = NULL;
            Result.push_back(TmpNode);
            return Result;
        }

        if (n == 1)
        {
            TreeNode* TmpNode = new TreeNode(n + base);
            Result.push_back(TmpNode);
            return Result;
        }

        for (int HeadIndex = 1; HeadIndex <= n; HeadIndex++)
        {
            vector<TreeNode*> LeftChildVector = generateTrees(HeadIndex - 1, base);
            vector<TreeNode*> RightChildVector = generateTrees(n - HeadIndex, base + HeadIndex);

            const vector<TreeNode*>::size_type LEFTSIZE = LeftChildVector.size();
            const vector<TreeNode*>::size_type RIGHTSIZE = RightChildVector.size();

            for (vector<TreeNode*>::size_type IndexOfLeft = 0; IndexOfLeft < LEFTSIZE; IndexOfLeft++)
            {
                for (vector<TreeNode*>::size_type IndexOfRight = 0; IndexOfRight < RIGHTSIZE; IndexOfRight++)
                {
                    TreeNode *TmpHeadNode = new TreeNode(base + HeadIndex);
                    TmpHeadNode->left = LeftChildVector[IndexOfLeft];
                    TmpHeadNode->right = RightChildVector[IndexOfRight];

                    Result.push_back(TmpHeadNode);
                }
            }
        }

        return Result;

    }

public:
    vector<TreeNode*> generateTrees(int n)
    {
        return generateTrees(n, 0);
    }
};

四.体会

这道题也是一个和Unique Binary Search Trees类似的问题,解题思路也比较类似,只是这个的返回是所有可能的二叉查找树,主要解答过程只是稍微修改下即可。

版权所有,欢迎转载,转载请注明出处,谢谢

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 10:17:13

LeetCode_Unique Binary Search Trees II的相关文章

[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 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

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

41. Unique Binary Search Trees &amp;&amp; Unique Binary Search Trees II

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 思路: f(n) = Σi=1n f(n-i)

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 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,#

leetcode_95_Unique Binary Search Trees II

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢 95 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