class Solution { public boolean verifyPostorder(int[] postorder) { int n = postorder.length; if(n<=1) return true; int j = n-1; for(;j>=0;j--){ if(postorder[j]<postorder[n-1]){ break; } } int[] left = Arrays.copyOfRange(postorder,0,j+1); int[] right = Arrays.copyOfRange(postorder,j+1,n-1); return track(postorder[n-1],left,right); } private boolean track(int root, int[] left, int[] right) { if(left.length>0){ for (int value : left) { if (value > root) { return false; } } } if(right.length>0){ for (int value : right) { if (value < root) { return false; } } } return verifyPostorder(left) && verifyPostorder(right); } }
递归。
单调栈的想法还不会实现。