重建二叉树

  直接上代码了

struct BTNode {
    int val;
    BTNode *lchild;
    BTNode *rchild;
    BTNode(int x): val(x), lchild(NULL), rchild(NULL){}
};

BTNode* rebuildBinTree(int a[], int b[], int n)
{
    if (n <= 0)
        return NULL;
    BTNode *root = new BTNode(a[0]);
    int i;
    for (i = 0; i < n; ++i)
    {
        if (b[i] == a[0])
            break;
    }
    root->lchild = rebuildBinTree(a+1, b, i);
    root->rchild = rebuildBinTree(a+i+1, b+i+1,n-i-1);
    return root;
}

对应文章:http://www.cnblogs.com/youxin/p/3288261.html

posted @ 2015-07-15 20:36  Sawyer Ford  阅读(146)  评论(0编辑  收藏  举报