递归思想 每个点都当做跟结点去中序数组中划分左右子树
HashMap<Integer,Integer> map = new HashMap<>();
private TreeNode build(int[] pre ,int l1 ,int r1 ,int l2 ,int r2){
if (l1 > r1) return null;
TreeNode root = new TreeNode(pre[l1]);
int in_root = map.get(pre[l1]);
int left = in_root - l2;
root.left = build(pre,l1+1 ,l1+left, l2,in_root - 1);
root.right = build(pre,l1+left+1 ,r1 ,in_root+1 ,r2);
return root;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
for (int i = 0; i < inorder.length; i++)
map.put(inorder[i],i);
return build(preorder,0,preorder.length-1,0,preorder.length-1);
}