import java.util.*;
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private Map<Integer,Integer> indexs = new HashMap<>();
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
for(int i = 0; i < vin.length; i++){
indexs.put(vin[i],i);
}
return helper(pre,vin,0,vin.length-1);
}
int i = 0;
// [left,right];
private TreeNode helper(int[] pre,int[] vin,int left,int right){
if(left > right) return null;
TreeNode root = new TreeNode(pre[i]);
int index_mid = indexs.get(pre[i]);
i++;
root.left = helper(pre,vin,left,index_mid-1);
root.right = helper(pre,vin,index_mid+1,right);
return root;
}
}