class Solution { public: bool VerifySquenceOfBST(vector<int> sequence) { vector<int> right; vector<int> left; int size = sequence.size(); int last = sequence[size - 1]; if (size== 0) { return false; } //left int mid = 0; for (int i = 0; i<(size); ++i) { if (sequence[i] < last) { left.push_back(sequence[i]); ++mid; } else break; } //right for (int j = mid; j < (size-1); ++j) { if (sequence[j]>last) { right.push_back(sequence[j]); } else { return false; break; } } bool leftresult=true; bool rightresult=true; if (!left.empty()) { bool leftresult = VerifySquenceOfBST(left); } if (!right.empty()) { bool rightresult = VerifySquenceOfBST(right); } return(leftresult&&rightresult); } };
时间: 2024-10-30 01:11:44