2 unique Binary Search Trees ii_leetcode

Source: Internet
Author: User

GivenN, Generate all structurally uniqueBST's(Binary Search Trees) that store values 1...N.

For example,
GivenN= 3, your program shocould return all 5 unique BST's shown below.

1 3 3 2 1

\///\\

3 2 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;    }};

 

2 unique Binary Search Trees ii_leetcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.