前序中序求後序,中序後續求前序

求後續

#include <bits/stdc++.h>

using namespace std;

int const maxn=1100;

int a[8][8];
int vis[8];
vector<string> ans;

void f(string ins,string post)
{
    if(ins.size()==0) return ;

    char node =post[post.size()-1];
       cout<<node;
    post = post.substr(0,post.size()-1);
    int po=ins.find(node);
    string post1 = post.substr(0,po);
    string post2 = post.substr(po);
    string ins1 = ins.substr(0,po);
    string ins2 = ins.substr(po+1);
    f(ins1,post1);
    f(ins2,post2);

}


int main()
{
   string s1,s2;
   while(cin>>s1>>s2)
   {
       f(s1,s2);
       cout<<endl;
   }

}

求前序

#include <bits/stdc++.h>

using namespace std;

int const maxn=1100;

int a[8][8];
int vis[8];
vector<string> ans;

void f(string pre,string ins)
{
    if(pre.size()==0) return ;
    char node =pre[0];
    pre = pre.substr(1);
    int po=ins.find(node);
    string pre1 = pre.substr(0,po);
    string pre2 = pre.substr(po);
    string ins1 = ins.substr(0,po);
    string ins2 = ins.substr(po+1);
    f(pre1,ins1);
    f(pre2,ins2);
    cout<<node;
}


int main()
{
   string s1,s2;
   while(cin>>s1>>s2)
   {
       f(s1,s2);
       cout<<endl;
   }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章