这题是第二次做了,两次都不是独立完成,不过我发现我第一次参考的程序,也是参考老师(陈越)的范例做出来的。我对老师给的做了小幅修改,因为我不想有全局变量的的存在,所以我多传了三个参数进去。正序遍历每次都是从1到N吗?看题目我认为应该是,结果我错了,我是对比正确的程序一点点修改才发现的,不容易啊。下面是题目及程序

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct
  6. {
  7. int * a;
  8. int top;
  9. }SeqStack;
  10.  
  11. void push(SeqStack * pS, int X);
  12. int pop(SeqStack * pS);
  13. void solve(int * pre, int * in, int * post, int preL, int inL, int postL, int n);
  14.  
  15. int main()
  16. {
  17. // freopen("in.txt", "r", stdin); // for test
  18. int i, N;
  19. scanf("%d", &N);
  20.  
  21. SeqStack S;
  22. S.a = (int *)malloc(N * sizeof(int));
  23. S.top = -;
  24. int pre[N], in[N], post[N];
  25.  
  26. char chars[];
  27. char * str = chars;
  28. int X, pre_index, in_index;
  29. pre_index = in_index = ;
  30. for(i = ; i < * N; i++)
  31. {
  32. scanf("%s", str);
  33. if(strcmp(str, "Push") == )
  34. {
  35. scanf("%d", &X);
  36. pre[pre_index++] = X;
  37. push(&S, X);
  38. }
  39. else
  40. in[in_index++] = pop(&S);
  41. }
  42.  
  43. solve(pre, in, post, , , , N);
  44. for(i = ; i < N; i++)
  45. {
  46. printf("%d", post[i]);
  47. if(i < N - )
  48. printf(" ");
  49. else
  50. printf("\n");
  51. }
  52. // fclose(stdin); // for test
  53. return ;
  54. }
  55.  
  56. void push(SeqStack * pS, int X)
  57. {
  58. pS->a[++(pS->top)] = X;
  59. }
  60.  
  61. int pop(SeqStack * pS)
  62. {
  63. return pS->a[pS->top--];
  64. }
  65.  
  66. void solve(int * pre, int * in, int * post, int preL, int inL, int postL, int n)
  67. {
  68. int i, root, L, R;
  69.  
  70. if(n == )
  71. return;
  72. if(n == )
  73. {
  74. post[postL] = pre[preL];
  75. return;
  76. }
  77. root = pre[preL];
  78. post[postL + n - ] = root;
  79. for(i = ; i < n; i++)
  80. if(in[inL + i] == root)
  81. break;
  82. L = i;
  83. R = n - L - ;
  84. solve(pre, in, post, preL + , inL, postL, L);
  85. solve(pre, in, post, preL + L + , inL + L + , postL + L, R);
  86. }

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

  1. 6
  2. Push 1
  3. Push 2
  4. Push 3
  5. Pop
  6. Pop
  7. Push 4
  8. Pop
  9. Pop
  10. Push 5
  11. Push 6
  12. Pop
  13. Pop

Sample Output:

  1. 3 4 2 6 5 1

03-树2 Tree Traversals Again的更多相关文章

  1. PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列

    Tree Traversals Again Tree Traversals Again 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的 ...

  2. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  4. 树-伸展树(Splay Tree)

    伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...

  5. HDU1710Binary Tree Traversals

    HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...

  6. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. PAT1086:Tree Traversals Again

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  8. Binary Tree Traversals(HDU1710)二叉树的简单应用

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  10. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

随机推荐

  1. SAP MM事务代码清单

  2. dedecms5.7怎么取消邮箱验证以及dedecms 会员发布的文章不需要审核的解决方法

    后台 ——系统基本参数——会员设置——会员权限开通状态——改为0 1.实现会员发布文章不需要审核,非会员发布需要审核 在member这个文件夹下找到archives_sg_add.php这个文件,打开 ...

  3. 【转】 SIFT算法详解

    尺度不变特征变换匹配算法详解Scale Invariant Feature Transform(SIFT)Just For Fun zdd  zddmail@gmail.com 对于初学者,从Davi ...

  4. js简单模仿队列

    window.meng = window.meng || {}; (function () { var items = []; meng.queue = { /** * * @param {Funct ...

  5. Windows Azure免费空间如何搭建PHP网站/数据库、域名绑定

    7月份,阿象为大伙介绍了中国版Windows Azure如何建站.自定义远程虚拟机,最高可选四核.28G内存的服务器,相信不少站长.开发者用户大呼过瘾.不过Azure建站系统仅支持SQL数据库,并不支 ...

  6. VBA中四种自动运行的宏以及模块的含义

    在Excel的“标准模块”中可以创建4种自动运行的宏,它们分别是Auto_Open(打开工作 簿时自动运行), Auto_Close, Auto_Activate,  Auto_Deactivate. ...

  7. Python中一些内建函数及os等模块的用法

          len(obj)                   # 求长度:obj可以是str.list等对象    split(str, num)          # str-分割符,默认空格: ...

  8. hdu----(2848)Repository(trie树变形)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  9. 并发容器之CopyOnWriteArrayList

    原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容 ...

  10. httpClient 4.x post get方法

    public static String doPost(String url, String encoding, String contentType, String sendData) throws ...