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;
}

posted on 2008-08-14 20:20 Victordu 阅读(819) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(5)

随笔档案(46)

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜