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
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);
}