题目

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.

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:

6

Push 1

Push 2

Push 3

Pop

Pop

Push 4

Pop

Pop

Push 5

Push 6

Pop

Pop

Sample Output:

3 4 2 6 5 1

题目分析

已知非递归中序(借助栈)遍历树的操作流程,求后序序列

注:操作流程中push操作依次组成前序序列,又可以借助push和pop操作得到中序序列,题目即转换为中序+前序->后序

解题思路

思路 01

  1. 中序+前序建树
  2. 递归后序遍历树

思路 02(最优)

  1. 中序+前序直接转后序序列

知识点

  1. while(~scanf("%s",s)) {} //等价于scanf("%s",s)!=EOF

两者作用是相同的

~是按位取反

scanf的返回值是输入值的个数

如果没有输入值就是返回-1

-1按位取反结果是0

while(~scanf("%d", &n))就是当没有输入的时候退出循环

EOF,为End Of File的缩写,通常在文本的最后存在此字符表示资料结束。

EOF 的值通常为 -1

Code

Code 01

  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. using namespace std;
  5. vector<int> pre,in;
  6. int index,n;
  7. struct node {
  8. int data;
  9. node * left;
  10. node * right;
  11. };
  12. node * create(int preL,int preR,int inL,int inR) {
  13. if(preL>preR)return NULL;
  14. node * now = new node;
  15. now->data=pre[preL];
  16. int k=inL;
  17. while(k<inR&&in[k]!=pre[preL])k++;
  18. now->left=create(preL+1, preL+(k-inL), inL, k-1);
  19. now->right=create(preL+(k-inL)+1, preR, k+1, inR);
  20. return now;
  21. }
  22. void postOrder(node * root) {
  23. if(root==NULL)return;
  24. postOrder(root->left);
  25. postOrder(root->right);
  26. printf("%d",root->data);
  27. if(++index<n)printf(" ");
  28. }
  29. int main(int argc,char * argv[]) {
  30. int id;
  31. string s;
  32. stack<int> sc;
  33. scanf("%d",&n);
  34. int len=n<<1;
  35. for(int i=0; i<len; i++) {
  36. cin>>s;
  37. if(s=="Push") {
  38. scanf("%d",&id);
  39. pre.push_back(id);//preorder
  40. sc.push(id);
  41. }
  42. if(s=="Pop") {
  43. in.push_back(sc.top());
  44. sc.pop();
  45. }
  46. }
  47. node * root = create(0,n-1,0,n-1);
  48. postOrder(root);
  49. return 0;
  50. }

Code 02(最优)

  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. #include <cstring>
  5. using namespace std;
  6. vector<int> pre,in,post;
  7. int n;
  8. void postOrder(int preL,int preR,int inL,int inR){
  9. if(inL>inR)return;// preL>preR也可以
  10. int k=inL;
  11. while(k<inR&&in[k]!=pre[preL])k++;
  12. postOrder(preL+1, preL+(k-inL), inL, k-1);//先存放左子节点
  13. postOrder(preL+(k-inL)+1, preR, k+1, inR);//后存放右子节点
  14. post.push_back(pre[preL]); //再存放父节点
  15. }
  16. int main(int argc,char * argv[]) {
  17. int id;
  18. char s[5];
  19. stack<int> sc;
  20. scanf("%d",&n);
  21. while(~scanf("%s",s)) { //等价于scanf("%s",s)!=EOF
  22. if(strlen(s)==4) {
  23. //Push
  24. scanf("%d",&id);
  25. pre.push_back(id);
  26. sc.push(id);
  27. } else {
  28. //Pop
  29. in.push_back(sc.top());
  30. sc.pop();
  31. }
  32. }
  33. postOrder(0,n-1,0,n-1);
  34. for(int i=0;i<post.size();i++){
  35. printf("%d",post[i]);
  36. if(i!=post.size()-1)printf(" ");
  37. }
  38. return 0;
  39. }

PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]的更多相关文章

  1. 1086. Tree Traversals Again (25)-树的遍历

    题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...

  2. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  3. PAT Advanced 1020 Tree Traversals (25 分)

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

  4. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  5. PAT (Advanced Level) 1086. Tree Traversals Again (25)

    入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...

  6. 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

    题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...

  7. 1086. Tree Traversals Again (25)

    题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...

  8. PAT 甲级 1086 Tree Traversals Again

    https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...

  9. PAT Advanced 1147 Heaps (30) [堆,树的遍历]

    题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...

随机推荐

  1. ROS-1 : Ubuntu16.04中安装ROS Kinetic

    1.安装 ROS Kinetic仅支持Wily(Ubuntu 15.10).Xenial( Ubuntu16.04)和Jessie(Debian 8)的debian软件包. 1.1 配置Ubuntu ...

  2. SpringBoot-数据库连接信息配置

    SpringBoot-数据库连接信息配置 SpringBoot-数据库连接信息配置 ​ 在SpringBoot中提供了默认的数据库连接器-追光者HikariCP,我们只需要添加jdbc的启动器就会自动 ...

  3. maven package跳过测试

    mvn clean package -DskipTests 或者 mvn clean package -Dmaven.test.skip=true 区别 -DskipTests,不执行测试用例,但编译 ...

  4. vi/vim常用操作

    什么是vim? Vim是从 vi 发展出来的一个文本编辑器.代码补全.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用,和Emacs并列成为类Unix系统用户最喜欢的文本编辑器. vim的 ...

  5. 【测试】性能测试及性能测试工具Loadrunner

    性能测试简介 软件系统的性能包括很多方面,有执行效率,资源占用,系统稳定性,安全性,兼容性,可靠性,可扩展性等.这些都是可以衡量一个软件系统性能好坏的指标.而性能测试是指通过自动化测试工具去模拟多种正 ...

  6. c++输出哈夫曼编码

    #include<iostream> #include<string> using namespace std; struct huffTree { int parent; i ...

  7. Ubuntu上安装tftp服务

    1. 安装 sudo apt install tftpd-hpa 2.设置工作目录 mkdir ~/tftpdroot tftpdroot 3.修改配置文件 sudo vi /etc/default/ ...

  8. 073-PHP数组元素相加

    <?php $arr1=array(1,2,3,4,'5','05',TRUE); //等价于 1+2+3+4+5+5+1=21 $arr2=array(1,2,'ABC',3,'hello', ...

  9. selenium2Library无法启动chrome

    使用其他浏览器都没有影响,唯独chrome启动不起来,去掉IE-连接-局域网设置-自动检测设置就OK了

  10. python 列表和字符串

    python 列表中保留所有字符串前三项,并保存到一个新的列表l = [s[:3] for s in data] python 在列表中查找包含所以某个字符串的项,并保存到一个新的列表l = [s f ...