/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function reConstructBinaryTree(pre, vin)
{
    // write code here
    if (!pre.length || !vin.length) return null;
    let root = pre[0];
    let index=vin.indexOf(root);
     //3、让左子树和右子树进行1、2步的递归操作,来构建左子树和右子树

        let leftTree=reConstructBinaryTree(pre.slice(1,index+1),vin.slice(0,index));
        let rightTree=reConstructBinaryTree(pre.slice(index+1),vin.slice(index+1));
    let newTree =  new TreeNodes(root);
    newTree.left=leftTree;
        newTree.right=rightTree;
        return newTree;

}
function TreeNodes(x) {
    this.val = x;
    this.left = null;
    this.right = null;

}
module.exports = {
    reConstructBinaryTree : reConstructBinaryTree
};