【hiho一下 第十周】后序遍历
【题目链接】:http://hihocoder.com/problemset/problem/1049
【题意】
【题解】
前序遍历的第一个节点;
肯定是整颗树的头结点;
然后在中序遍历中;
得到这个树的左子树和右子树;
然后再分别得到左子树和右子树的前序遍历;
递归处理就好;
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;
int root,l[30],r[30];
string s1,s2;
int dfs(string s1,string s2)
{
if (s1==""||s2=="") return 0;
char t = s1[0];
int x = t-'A'+1;
int pos = s2.find(t,0);
int llen = pos,rlen = s2.size()-llen-1;
l[x] = dfs(s1.substr(1,llen),s2.substr(0,llen));
r[x] = dfs(s1.substr(1+llen,rlen),s2.substr(pos+1,rlen));
return x;
}
void dfs(int x)
{
if (x==0) return;
dfs(l[x]);dfs(r[x]);
char t = x+'A'-1;
cout << t;
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
cin >> s1 >> s2;
root = dfs(s1,s2);
dfs(root);
cout << endl;
return 0;
}