合法二叉树后序遍历序列
转载请注明作者与出处:franciscolv http://www.cnblogs.com/franciscolv/archive/2011/11/20/2255838.html
package SortSet;
/**
* 判断输入数组是否为合格的二叉搜索树后续遍历结果
*
* @author franciscolv
*
*/
public class VerifyPosOrder {
public static void main(String[] args) {
int[] a = { 2, 3, 9, 11, 10, 8 };
System.out.println(verifyPosOrder(a, 0, a.length -1));
}
public static boolean verifyPosOrder(int[] a, int s, int e) {
if (a == null || s > e)
return false;
if (s == e)
return true;
int root = a[e];
int i = s;
for (; i < e; i++)
if (a[i] > root)
break;
for (int j = i + 1; j < e; j++)
if (a[j] < root)
return false;
boolean left = true, right = true;
if (i > 0)
left = verifyPosOrder(a, s, i - 1);
if (i < e)
right = verifyPosOrder(a, i, e - 1);
return left && right;
}
}