这道题主要考察对后顺遍历的理解。
具体分析参考剑指offer33.
时间和内存消耗以及源代码如下:
bool verifyPostorder(int* postorder, int postorderSize){
if (postorder == NULL || postorderSize == 0)
{
return true;
}
if (postorderSize == 1)
{
return true;
}
int root = postorder[postorderSize-1];
int left = 0;
while (postorder[left] < root)
{
left++;
}
int right = 0;
while (postorder[right+left] > root)
{
right++;
}
if (postorder[right+left] == root)
{
if (left == 0 || left == (postorderSize-1))
{
return verifyPostorder(postorder, (postorderSize - 1));
}
return verifyPostorder(postorder, left) &&
verifyPostorder(&(postorder[left]), right);
// verifyPostorder(postorder+left*sizeof(int), right);
}
return false;
}