您好我正在打印预订单树遍历,只有顺序和后序遍历。这样做的代码如下:
public static void preorder( int inorder[], int inostart, int postorder[], int poststart, int length)
{
if(length==0) return; //terminating condition
int k=0;
System.out.print(postorder[length-1] + " ");
for(int i=inostart; i<inostart+length; i++)
{
if(postorder[poststart+length-1]==inorder[i])
{
k=i;
break;
}
}
preorder(inorder, inostart, postorder, poststart, poststart+k-(inostart+1));
preorder(inorder, inostart+k-poststart+1, postorder, k+1, length-k+inostart-1);
}
但我得Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
来测试数字
POSTORDER
4 2 7 5 9 8 6 3 1
序
4 2 1 5 7 3 6 8 9