1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. const int MAXN = 27;
  5. struct node{
  6. char data;
  7. node* lchild;
  8. node* rchild;
  9. //node(char c): data(c),lchild(NULL),rchild(NULL){};
  10. };
  11. int pre[MAXN], in[MAXN];
  12. node* create(int preL, int preR, int inL, int inR){
  13. if(preL > preR){
  14. return NULL;
  15. }
  16. node* root = new node;
  17. root->data = pre[preL];
  18. int k;
  19. for(k = inL; k < inR; k++){
  20. if(in[k] == pre[preL]){
  21. break;
  22. }
  23. }
  24. int numLeft = k-inL;
  25. root -> lchild = create(preL+1, preL + numLeft, inL, k-1);
  26. root -> rchild = create(preL+numLeft+1, preR, k+1, inR);
  27. return root;
  28. }
  29. void postorder(node* root){
  30. if(root == NULL){
  31. return;
  32. }
  33. postorder(root->lchild);
  34. postorder(root->rchild);
  35. printf("%c", root->data);
  36. }
  37. int main(){
  38. string str;
  39. while(getline(cin, str)){
  40. for(int i=0;i<str.size();i++){
  41. pre[i]=str[i];
  42. }
  43. getline(cin, str);
  44. for(int i=0;i<str.size();i++){
  45. in[i]=str[i];
  46. }
  47. node* root = create(0, str.size()-1, 0, str.size()-1);
  48. postorder(root);
  49. cout<<endl;
  50. memset(pre, 0, sizeof(pre));
  51. memset(in, 0, sizeof(in));
  52. }
  53. return 0;
  54. }
点赞(0)
 

0 分

0 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论