164. Different Binary Search Trees II

164. Different Binary Search Trees II
 

Given n, generate all different binary search trees composed of 1...n nodes.
Example

Example 1:

Input: 3
Output:
    1 3 3 2 1
     \ / / / \ \
      3 2 1 1 3 2
     / / \ \
    2 1 2 3

Precautions

n>=1

 

 

 void print(int&src,vector<int>&dst, vector<int>&visited, vector<vector<int> >&ret )
 {
     for (int i = 1; i <= src; i++)
     {
         if (visited[i] == 1) //已访问过
         {
             continue;
         }
         visited[i] = 1;
         dst.push_back(i);
         if (dst.size() == src)
         {
             ret.push_back(dst);
             dst.pop_back();
             visited[i] = 0;
             return;
         }
         print(src, dst, visited, ret);
         dst.pop_back();
         visited[i] = 0;
     }
 }

 bool isTree(TreeNode * &root, int num)
 {
     if (nullptr == root)
     {
         TreeNode * node = new TreeNode(num);
         root = node;
         return true;
     }
     TreeNode * tmp = root;
     //while (tmp->left != nullptr && tmp->right != nullptr)
     while (tmp != nullptr )
     {
         if (tmp->val > num)
         {
             if (tmp->left == nullptr)
             {
                 TreeNode * node = new TreeNode(num);
                 tmp->left = node;
                 return true;
             }
             tmp = tmp->left;
            /* if (nullptr != tmp && tmp->val < num)
             {                  return false;              }*/          }          else if (tmp->val < num)          {              if (tmp->right == nullptr)              {                  TreeNode * node = new TreeNode(num);                  tmp->right = node;                  return true;              }             tmp =  tmp->right;             //if (nullptr != tmp && tmp->val < num) //(1 3 2 ) (1 2 3)             //{             //    return false;             //}          }          else if (tmp->val == num)


















         {
             return false;
         }
     }
 }

void floor(vector<int > &dst, TreeNode * root)
{
    if (nullptr == root)
        return;
    queue<TreeNode * > q;
    q.push(root);
    while (!q.empty())
    {
        TreeNode * node = q.front();
        q.pop();
        dst.push_back(node->val);
        if (nullptr != node->left)
        {
            q.push(node->left);
        }
        else
        {
            dst.push_back(-1);
        }
        if (nullptr != node->right)
        {
            q.push(node->right);
        }
        else
        {
            dst.push_back(-1);
        }
    }
}

void floorPrint(vector<vector<int>>&dst2, std::vector<TreeNode *> retVec)
{
    for (int i = 0; i < retVec.size(); i++)
    {
        TreeNode *root = retVec[i];
        vector<int>dst;
        floor(dst, root);
        dst2.push_back(dst);
    }
}

//全排序,按层遍历去重
std::vector<TreeNode *> generateTrees(int n)
{
    vector<int>dst;
    vector<int>visited(n + 1, 0);
    vector<vector<int> >ret;
    print(n, dst, visited, ret);
    std::vector<TreeNode *> retVec;
    for (int i = 0; i < ret.size(); i++)
    {
        vector<int> tmp = ret[i];
        TreeNode * root = nullptr;
        bool bFind = true;
        for (int j = 0; j < tmp.size(); j++)
        {
            if (false == isTree(root, tmp[j]))
            {
                bFind = false;
                break;
            }
        }
        if (true == bFind)
        {
            retVec.push_back(root);
        }
    }
    vector<vector<int>>floorVec;
    floorPrint(floorVec, retVec);
    map<vector<int>, int> floorMap;
    set<vector<int>> floorSet;
    for (int i = 0; i < floorVec.size(); i++)
    {
        floorMap[floorVec[i]] = i;
        floorSet.insert(floorVec[i]);
    }
    std::vector<TreeNode *> retVec2;
    for (auto it : floorSet)
    {
        auto itt = floorMap.find(it);
        if (itt != floorMap.end())
        {
            retVec2.push_back(retVec[itt->second]);
        }
    }

    return retVec2;
}


void test()
{
    int n = 3;
    std::vector<TreeNode *> ret = generateTrees(n);
}

 

Guess you like

Origin blog.csdn.net/yinhua405/article/details/111660833