阿里云优惠活动,点击链接进行购买: 一年仅需89元即可以购买服务器~。我个人服务器为2核4G配置,也更加推荐购买 2核4G三年799元 配置的服务器。

# 二叉树之重建(js)

输入一颗二叉树的先序遍历和中序遍历,输出它的后序遍历。 输入: DBACEGF ABCDEFG BCAD CBAD 输出: ACBFGED CDAB

	var root = {value: null, left: null, right: null};
    function build(root, s1, s2) {
        if (s1.length === 0 || s2.length === 0 ) {
            return;
        }

        var midnode = s2.indexOf(s1[0]);
        var mleft = s2.substring(0, midnode),
                mright = s2.substring(midnode + 1);
        var pleft = s1.substring(1, mleft.length + 1),
                pright = s1.substring(mleft.length + 1);
        root.value = s1[0];
        root.left = newchild();
        root.right = newchild();

        if(root.value) {
            build(root.left, pleft, mleft);
            build(root.right, pright, mright);
        }
    }
    function newchild() {
        return {value: null, left: null, right: null}
    }
    build(root, 'DBACEGF', 'ABCDEFG');

    function postrav(root) {
        if(root.value!==null){
            postrav(root.left);
            postrav(root.right);
            console.log(root.value);
        }

    }
    postrav(root);
Last Updated: 3/29/2020, 11:51:17 PM