After sequence preorder sequence under construction, wherein a

node *build(int pre_l, int pre_r, int post_l, int post_r) {
    if(pre_l > pre_r) return NULL;
    if(pre_l == pre_r) return new node(pre[pre_l]);
    node *n = new node(pre[pre_l]);
    int k = post_l;
    while(k < post_r && post[k] != pre[pre_l + 1]) k++;
    n->left = build(pre_l + 1, pre_l + 1 + k - post_l, post_l , k);
    n->right = build(pre_l + 1 + k - post_l + 1, pre_r, k + 1, post_r - 1);
    return n;
}

Thank soft hospital group Xiao Han and Fermat's help

Guess you like

Origin www.cnblogs.com/littlepage/p/12664195.html