/*
    给出后序中序还原二叉树,并写出前序遍历

    后序遍历: FGCDEBA

    中序遍历: FCGADBE

    /

    /*
    A
    C B
    F G D E


    前序遍历: ACFGBDE
    /

    var hou = [‘f’, ‘g’, ‘c’, ‘d’, ‘e’, ‘b’, ‘a’];
    var zhong = [‘f’, ‘c’, ‘g’, ‘a’, ‘d’, ‘b’, ‘e’];

    function Node(value) {
    this.value = value;
    this.left = null;
    this.right = null;
    }

    function f1(zhong, hou) {
    if (hou == null || zhong == null || hou.length == 0 || zhong.length == 0 || hou.length != zhong.length) return null;

    1. var root = new Node(hou[hou.length - 1]);<br /> var index = zhong.indexOf(root.value); // 找到根节点在中序遍历中的位置<br /> var houLeft = hou.slice(0, index); //后序遍历的左子树<br /> var houRight = hou.slice(index, hou.length - 1); //后序遍历的右子树
    2. var zhongLeft = zhong.slice(0, index);//中序遍历的左子树<br /> var zhongRight = zhong.slice(index + 1, zhong.length);//中序遍历的右子树<br /> root.left = f1(zhongLeft, houLeft);//根据左子树的后序和中序还原左子树并赋值给root.left<br /> root.right = f1(zhongRight,houRight);//根据右子树的后序和中序还原右子树并赋值给root.right<br /> return root;<br />}

    var root = f1(zhong, hou);

    console.log(root.left);
    console.log(root.right);