剣指offer二叉探索木の後序遍歴シーケンス
分治再帰.
class Solution {
public:
bool VerifySquenceOfBST(vector<int> sequence) {
int len = sequence.size();
if(len == 0)
return false;
if(len == 1)
return true;
vector<int> left;
vector<int> right;
int middle = sequence[len - 1];
int num = 0;
while(sequence[num] < middle)
left.push_back(sequence[num++]);
while(num < len - 1)
{
if(sequence[num] < middle)
return false;
right.push_back(sequence[num++]);
}
if(left.size() == 0)
return VerifySquenceOfBST(right);
if(right.size() == 0)
return VerifySquenceOfBST(left);
return VerifySquenceOfBST(left) && VerifySquenceOfBST(right);
}
};