Sword refers to the post-order traversal sequence of Offer-33 binary search tree

public static boolean verifyPostorder(int[] postorder) {
    return recur(postorder, 0, postorder.length - 1);
}

public static boolean recur(int[] postorder, int i, int j) {
    if(i >= j) {
        return true;
    }
    int p = i;
    // Search the binary tree to find the left subtree of the current root node
    while(postorder[p] < postorder[j]) {
        p++;
    }
    // m divides the left and right subtrees
    int m = p;
    // find the right subtree
    while(postorder[p] > postorder[j]) {
        p++;
    }
    // Finally p==j determines whether the tree is a legal search binary tree
    return p == j && recur(postorder, i, m - 1) && recur(postorder, m, j - 1);
}

Guess you like

Origin blog.csdn.net/a792396951/article/details/113857753