我刚开始写的不完整版
class Solution {
public:
bool verifyPostorder(vector<int>& postorder) {
//数组的最后一个数是树的根节点
int length=postorder.size();
int lastele=postorder[length-1];
//左树所有值小于根节点 右树所有值大于根节点
vector<int> lefttree;
vector<int> righttree;
for(auto x:lefttree){
if(x<lefttop)
lefttree.push_back(x);
else
rightttree.push_back(x);//树中所有元素不同
}
for(auto x:righttree){
if(x<righttop)
lefttree.push_back(x);
else
rightttree.push_back(x);//树中所有元素不同
}
lefttop=lefttree.top();
righttop=righttree.top();
}
};
我是觉得肯定有个递归求解子树的过程 但是感觉写不出来
是否和归并排序很像的递归方法?
class Solution {
// 要点:二叉搜索树中根节点的值大于左子树中的任何一个节点的值,小于右子树中任何一个节点的值,子树也是
public:
bool verifyPostorder(vector<int>& postorder) {
if (postorder.size() < 2) return true;
return verify(postorder, 0, postorder.size() - 1);
}
// 递归实现
bool verify(vector<int>& postorder, int left, int right){
if (left >= right) return true; // 当前区域不合法的时候直接返回true就好
int rootValue = postorder[right]; // 当前树的根节点的值
int k = left;
while (k < right && postorder[k] < rootValue){ // 从当前区域找到第一个大于根节点的,说明后续区域数值都在右子树中
k++;
}
for (int i = k; i < right; i++){ // 进行判断后续的区域是否所有的值都是大于当前的根节点,如果出现小于的值就直接返回false
if (postorder[i] < rootValue) return false;
}
// 当前树没问题就检查左右子树
if (!verify(postorder, left, k - 1)) return false; // 检查左子树
if (!verify(postorder, k, right - 1)) return false; // 检查右子树
return true; // 最终都没问题就返回true
}
};
这个和我想得方法比较像