[UVA 10701]Pre,in and post (二叉树)(dfs)

来源:互联网 发布:淘宝客自动采集api 编辑:程序博客网 时间:2024/06/05 20:51

题目大意:
得到中左右,左中右,求左右中。
思路:
缓冲一下用dfs求出中右左并用stack储存就可以。

真情实感的写代码都是会得到好运的。
瞧,和 UVA 536 Tree Recovery 一毛一样。
我写的好像跟常规不太像要再研究下别人的。


/*uva 10706 Pre,in and post (dfs) by zhuhuaTime limit: 3000msAC time: 0ms???*/#include <iostream>#include <stack>using namespace std;string precord,incord;stack <char> ReOut;void dfs(string a,string b){    if(a=="")return;    char head=a[0];    ReOut.push(head);    int pos=b.find(head,0);    //cout<<head<<endl;    string lefta,leftb,righta,rightb;    righta=a.substr(pos+1,a.length()-pos-1);    rightb=b.substr(pos+1,b.length()-pos-1);    //cout<<righta<<" "<<rightb<<endl;    dfs(righta,rightb);    lefta=a.substr(1,pos);    leftb=b.substr(0,pos);    dfs(lefta,leftb);}int main(){int t,len;cin>>t;    while(cin>>len>>precord>>incord)        //pre:root,left,right        //in:left,root,right    {        dfs(precord,incord);        while(!ReOut.empty())        {            cout<<ReOut.top();            ReOut.pop();        }cout<<endl;    }    return 0;}
原创粉丝点击