经典二叉树问题 根据前序和中序生成后序
转载请注明作者与出处:franciscolv http://www.cnblogs.com/franciscolv/archive/2011/11/20/2255868.html。
package SortSet;
/**
* 根据前序和中序打印后序
*
* @author Administrator
*
*/
public class BinarayTreeOrder {
private static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
int[] inorder = { 1, 5, 4, 2, 3, 8, 7 };
int[] preorder = { 2, 5, 1, 4, 8, 3, 7 };
printPosOrder(preorder,inorder,0, 6, 0, 6);
sb.deleteCharAt(sb.length() - 1);
System.out.println(sb.toString());
}
public static void printPosOrder(int[]preorder,int[] inorder,int preleft, int preright, int inleft,
int inright) {
int parent, leftsize, rightsize;
if (preleft <= preright && inleft <= inright) {
for (parent = inleft; parent <= inright; parent++) {
if (inorder[parent] == preorder[preleft])
break;
}
leftsize = parent - inleft;
rightsize = inright - parent;
if (leftsize > 0) {
printPosOrder(preorder,inorder,preleft + 1, preleft + leftsize, inleft,
parent - 1);
}
if (rightsize > 0) {
printPosOrder(preorder,inorder,preleft + leftsize + 1, preright, parent + 1,
inright);
}
sb.append(inorder[parent]).append(",");
}
}
}