2 Unique Binary Search Trees II_Leetcode

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

This is the second time I solve this problem.

Everytime when we encounter a BST problem without quite clear thought, we can resort to Divide & Conquer.

Use recursion to build the left and right subtree, then combine them then return.

In this problem, the left and right subtree can be multiple.

FIRST TRY ERROR: Forget to clear the vector of left of right subtree while apply different root.

Code:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<TreeNode *> generateTrees(int n) {
        vector<TreeNode *> res;
        if(n == 0)
        {
            res.push_back(NULL);
            return res;
        }

        res = gen(1, n);
        return res;
    }

    vector<TreeNode *> gen(int start, int end)
    {
        vector<TreeNode*> res;
        if(start == end)
        {
            TreeNode* tmp = new TreeNode(start);
            res.push_back(tmp);
            return res;
        }

        vector<TreeNode*> leftsub, rightsub;
        for(int i = start; i <= end; i++)
        {
            leftsub.clear();  // First try error
            rightsub.clear();  // First try error

            if(i == start) leftsub.push_back(NULL);
            else leftsub = gen(start, i-1);

            if(i == end) rightsub.push_back(NULL);
            else rightsub = gen(i+1, end);

            for(int m = 0; m < leftsub.size(); m++)
            {
                for(int n = 0; n < rightsub.size(); n++)
                {
                    TreeNode* root = new TreeNode(i); // divide & conquer
                    root->left = leftsub[m];
                    root->right = rightsub[n];
                    res.push_back(root);
                }
            }
        }

        return res;
    }
};

  

时间: 2024-11-09 00:31:32

2 Unique Binary Search Trees II_Leetcode的相关文章

Unique Binary Search Trees I &amp; II

Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? 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 分析: 当数组为 1,2,3,4,.. i,.. n时,基于以下原则的BST建树具有唯一性:以i为根节点的树,其

[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 &amp;&amp; Unique Binary Search Trees II

1. 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 题目分析参考自一博

[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_96题——Unique Binary Search Trees(动态规划)

Unique Binary Search Trees Total Accepted: 48675 Total Submissions: 134436My Submissions Question Solution 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 uniq

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

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