
import java.util.HashMap;
public class PreInToTree {
public static TreeNode preInToTree(int[] pre,int[] in){
if(pre==null||in==null){
return null;
}
HashMap<Integer, Integer> map = new HashMap<>(8);
for(int i=0;i<in.length;i++){
map.put(in[i],i);
}
return preIn(pre,0,pre.length-1,in,0,in.length-1,map);
}
public static TreeNode preIn(int[] pre, int pl, int pr, int[] in, int il, int ir, HashMap<Integer,Integer> map){
if(pl>pr){
return null;
}
TreeNode head = new TreeNode(pre[pl]);
int index = map.get(pre[pl]);
head.left = preIn(pre, pl+1, pl + index - il, in, il, index-1, map);
head.right=preIn(pre,pl + index - il+1,pr,in,index+1,ir,map);
return head;
}
}