The backward traversal sequence of a binary search tree

Topic describes

Enter an array of integers to determine if the array is the result of a sequential traversal of a binary search tree. Returns true if it is, false otherwise. Suppose that any two numbers in the input array are different.

Title link: the trailing traversal sequence of a binary search tree

code

Enter an array of integers to determine if the array is the result of a binary search tree's post-traversal sequence. Returns true if it is, false otherwise. Suppose that any two numbers in the input array are different. * Title link: * https://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd?tpId=13&&tqId=11176&rp=1&ru=/ta/coding-interviews&qru =/ta/coding-interviews/question-ranking */
public class Jz23 {

    public boolean verifySquenceOfBST(int[] sequence) {
        if (sequence == null || sequence.length == 0) {
            return false;
        }
        return verify(sequence, 0, sequence.length - 1);
    }

    /** * recursion **@param sequence
     * @param first
     * @param last
     * @return* /
    private boolean verify(int[] sequence, int first, int last) {
        if (last - first <= 1) {
            return true;
        }
        int rootVal = sequence[last];
        int cutIndex = first;
        while (cutIndex < last && sequence[cutIndex] <= rootVal) {
            cutIndex++;
        }
        for (int i = cutIndex; i < last; i++) {
            if (sequence[i] < rootVal) {
                return false; }}return verify(sequence, first, cutIndex - 1) && verify(sequence, cutIndex, last - 1);
    }

    public static void main(String[] args) {}}Copy the code

Even if there is a strong wind, life does not give up. The wind, when courageously intention this life.