Leetcode 剑指 Offer 33 二叉搜索树的后序遍历序列

这道题主要考察对后顺遍历的理解。
具体分析参考剑指offer33.

时间和内存消耗以及源代码如下:
Leetcode 剑指 Offer 33 二叉搜索树的后序遍历序列_第1张图片

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;


}

你可能感兴趣的:(leetcode,c语言,算法)