POJ 2255 递归

POJ 2255 递归

这题要是做数据结构的练习题挺好的
就是给出前序和中序序列 要求后序序列
在先序序列中,第一个元素为二叉树的根,之后为它的左子树和右子树的先序序列;在中序序列中,先是左子树的中序序列,然后是根,再就是右子树的中序序列。由此就可以递归的建立起这棵二叉树了。
递归有时真的很美。。。

Node* create(const string& pres,const string& ins)
{
    Node* root;

    if(pres.length()>0)
    {
        root=new Node;
        root->data=pres[0];
        int index=ins.find(root->data);
        root->left=create(pres.substr(1,index),ins.substr(0,index));
        root->right=create(pres.substr(index+1),ins.substr(index+1));
    }
    else root=NULL;

    return root;
}

你可能感兴趣的:(POJ 2255 递归)