由中序遍历和后序遍历求前序遍历

佐手、 提交于 2020-03-09 07:30:37
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 30;
 8 char in[maxn], post[maxn];
 9 void Build_PostTree(char *in, char *post, int len)
10 {
11     if(len == 0) return;
12     cout << *(post + len - 1);
13     int i = 0;
14     for( ; i < len; i++)
15         if(in[i] == *(post + len - 1)) break;
16     Build_PostTree(in, post, i); //Left
17     Build_PostTree(in + i + 1, post + i, len - i - 1); // Right
18     return ;
19 }
20 int main()
21 {
22     freopen("in.txt","r",stdin);
23     while(scanf("%s %s", in, post) != EOF){
24         int len = strlen(post);
25         Build_PostTree(in, post, len);
26         cout << endl;
27     }
28     return 0;
29 }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!