public static BTNode preinOrder(int[] pre,int[] in,int prelow,int prehigh,int inlow,int inhigh){//前序和中序 if((prehigh-prelow>=0)&&(inhigh-inlow>=0)){ BTNode n=new BTNode(pre[prelow]); int k=inlow; for(int i=inlow;i<=inhigh;i++){ if(pre[prelow]==in[i]){ k=i-inlow; break; } } n.setLeft(preinOrder(pre,in,prelow+1,prelow+k,inlow,inlow+k-1)); n.setRight(preinOrder(pre, in, prelow+k+1, prehigh, inlow+k+1, inhigh)); return n; } else return null; } public static BTNode inpostOrder(int[] a,int[] b,int inlow,int inhigh,int postlow,int posthigh){//中序a和后序b if((posthigh-postlow>=0)&&(inhigh-inlow>=0)) { BTNode btNode = new BTNode(b[posthigh]); int k=0; for(int i=inlow;i<=inhigh;i++){ if(a[i]==b[posthigh]){ k=i-inlow;//距离inlow的距离 break; } } btNode.setLeft(inpostOrder(a, b, inlow , inlow+k-1, postlow, k +postlow-1)); btNode.setRight(inpostOrder(a, b, inlow+k+1, inhigh, k +postlow, posthigh-1)); return btNode; } else return null; }