import java.util.*;
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
        if (pre == null || vin == null)
            return null;
        if (pre.length == 0 || vin.length == 0)
            return null;
        if (pre.length != vin.length)
            return null;
        TreeNode root = new TreeNode(pre[0]);

        for (int i = 0; i < pre.length; i++) {
            if (root.val == vin[i]) {
                root.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1,
                        i + 1), Arrays.copyOfRange(vin, 0, i));
                root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,
                        i + 1, pre.length), Arrays.copyOfRange(vin, i + 1, vin.length));
            }
        }
        return root;
    }
}

主要就是递归的思想,前序遍历只用来确定根节点,然后在中序遍历里找到根节点左侧全是左子树右侧全是右子树,在前序遍历中,左右子树元素个数相同,分别找到左右子树的前序,中序,递归。 Arrays.copyOfRange是左闭右开