首页 > 代码库 > 举例让抽象问题具体化:二叉搜索树的后序遍历序列

举例让抽象问题具体化:二叉搜索树的后序遍历序列

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

public class Solution {    public boolean VerifySquenceOfBST(int [] sequence) {        if (sequence.length == 0) {            return false;        }        return judge(sequence, 0, sequence.length-1);    }    public boolean judge(int[] sequence, int left, int right) {        if (left >= right)            return true;        int i = right;        while (i > left && sequence[i - 1] > sequence[right])            --i;        for (int j = i - 1; j >= left; --j) {            if (sequence[j] > sequence[right]) {                return false;            }        }        return judge(sequence, left, i - 1) && judge(sequence, i, right - 1);    }}

 

举例让抽象问题具体化:二叉搜索树的后序遍历序列