Equality of two binary search trees constructed from unordered arrays

后端 未结 4 1977
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 02:24

Given two unsorted arrays of size N each, we are to determine if the Binary Search Tree constructed from them will be equal or not.

So, the elements of

4条回答
  •  一生所求
    2021-02-15 03:27

    I came up with following code. It works fine, though partitioning is inefficient.

        bool isBST (vector vec1, vector vec2) {
        if (vec1.size() == 0 && vec2.size() == 0)
            return true;
        if (vec1.size() != vec2.size())
            return false;
        if (vec1[0] != vec2[0])
            return false;
    
        vector temp1;
        vector temp2;
        vector temp3;
        vector temp4;
        for (int k = 1; k < vec1.size(); k++) {
           if(vec1[k] < vec1[0])
               temp1.push_back(vec1[k]);
           else
               temp2.push_back(vec1[k]);
    
           if(vec2[k] < vec2[0])
               temp3.push_back(vec2[k]);
           else
               temp4.push_back(vec2[k]);
        }
    
        return isBST(temp1, temp3) && isBST(temp2, temp4);
    
    }
    

提交回复
热议问题