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

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。

参考以下这颗二叉搜索树:

     5
    / \
   2   6
  / \
 1   3

示例 1:

输入: [1,6,3,2,5]
输出: false

示例 2:

输入: [1,3,2,6,5]
输出: true

提示:

  • 数组长度 <= 1000

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Solution 1

class Solution {
    public boolean verifyPostorder(int[] postorder) {
        return recursiveVerifyPostorder(postorder, 0, postorder.length);
    }

    // Verify if postorder[left:right] is binary search tree
    private boolean recursiveVerifyPostorder(int[] postorder, int left, int right) {
        if (left >= right - 1)
            return true;
        int root = postorder[right - 1];
        int split = 0;
        for (split = left; split < right - 1; split++) {
            if (postorder[split] > root)
                break;
        }
        for (int i = split; i < right - 1; i++) {
            if (postorder[i] < root)
                return false;
        }
        boolean result = recursiveVerifyPostorder(postorder, left, split)
                && recursiveVerifyPostorder(postorder, split, right - 1);
        return result;
    }
}