常见算法-二叉树的后序遍历序列
package common; /** * @author : zhaoliang * @program :newCoder * @description : 二叉树的后序遍历序列 * @create : 2020/11/27 19:10 */ public class VerifySquenceOfBST { //输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回true,否则返回false。 // 假设输入的数组的任意两个数字都互不相同。 public boolean VerifySquenceOfBST(int[] array){ if(array==null || array.length==0)return false; int start=0, end = array.length-1; return verify(array,start,end); } private boolean verify(int[] array, int start, int end) { if(end - start <=1)return true; int rootVal = array[end]; int curIndex = start; while(array[curIndex] <= rootVal && curIndex <end){ curIndex++; } for (int i = curIndex; i <end ; i++) { if(array[i] < rootVal){ return false; } } return verify(array,start,curIndex-1) && verify(array,curIndex,end-1); } }
下一篇:
队列的基本操作(C语言)