【题目】输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回 false。 【思路】根节点是最后一个节点,小于根节点的都是左子树,大于根节点的是右子树。再递归处理各个子树。 【代码】 //By proing [http://blog.youkuaiyun.com/proing] #include "stdafx.h" #include <iostream> using namespace std; //p数组,low开始位,high终值位 bool check(int p[], int low, int high) { if(low > high) return false; if(low == high) return true; //左子树 int i = 0; while( p[low+i] < p[high]) { i++; } if(i>0 && false == check(p,low,low+i-1)) // >0,表示有子树 return false; //右子树 low = low+i; i = 0; while( low+i< high) { if(p[low+i]<=p[high]) return false; i++; } if(i>0 && false == check(p,low,high-1)) // >0,表示有子树 return false; return true; }