验证二叉搜索树的后序遍历序列 (验证二叉搜索树的后序遍历序列)

 

思路:

// @Title: 验证二叉搜索树的后序遍历序列 (验证二叉搜索树的后序遍历序列)
// @Author: qisiii
// @Date: 2022-02-21 13:30:38
// @Runtime: 0 ms
// @Memory: 38.6 MB
// @comment: 
// @flag: 
class Solution {
    public static boolean verifyPostorder(int[] postorder) {
        if(postorder.length<=1){
            return true;
        }
        int root=postorder[postorder.length-1];
        int count=0;
        while(count<postorder.length-1){
            if(postorder[count++]>root){
                for (int i = count-1; i < postorder.length-1; i++) {
                   if (postorder[i]<root){
                       return false;
                   }
                }
                if (count==1){
                    break;
                }
                return verifyPostorder(Arrays.copyOfRange(postorder,0,count-1))&&verifyPostorder(Arrays.copyOfRange(postorder,count-1,postorder.length-1));
            }
        }
        return verifyPostorder(Arrays.copyOfRange(postorder,0,postorder.length-1));
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18