题目来自:pta题目集(无邀请码打不开)
题目如下:
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
输入样例:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

输出样例:

3 4 2 6 5 1

分析过程:

通过观察可以发现,元素入栈的顺序为该树的先序遍历,出栈 顺序为中序遍历,
那么这个问题便可转化为已知先序和中序遍历求后序遍历

代码如下:

#include <cstdio>
#include <time.h>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
char s[6],tt[6]="Push",r[6]="Pop";
int a[33],b[33],aaa=0,bbb=0,c[33],ccc=0;
struct node{int data;node*pre;node*left;node*right;
};
node* newnode(int key){node*t=(node*)malloc(sizeof(node));t->data=key;t->pre=NULL;t->left=NULL;t->right=NULL;return t;
}//创建新节点
void buildtree(int l,int r,int &t,node*&tree){int flag=-1;for(int i=l;i<=r;i++){if(b[i]==a[t]) {flag=i;break;}}if (flag==-1) return;tree=newnode(b[flag]);t++;if(flag>l) buildtree(l,flag-1,t,tree->left);if(flag<r) buildtree(flag+1,r,t,tree->right);
}
void post(node*tree){if(tree){post(tree->left);post(tree->right);c[ccc++]=tree->data;}
}
int main()
{   int n,m,k,cnt=0,t=0;scanf("%d",&n);stack<int>kkk;while(1){scanf("%s",s);if(strcmp(s,tt)==0){scanf("%d",&k);a[aaa++]=k;kkk.push(k);}else{b[bbb++]=kkk.top();kkk.pop();cnt++;}if(cnt==n) break;}node*root;buildtree(0,n-1,t,root);post(root);for(int i=0;i<n;i++){printf("%d",c[i]);if(i!=n-1) printf(" ");}return 0;
}

树的应用:Tree Traversals Again相关推荐

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

    import java.util.Scanner; import java.util.Stack; /** * @Author WaleGarrett * @Date 2020/9/5 12:02 * ...

  2. 03-树3 Tree Traversals Again(树的遍历)

    03-树3 Tree Traversals Again 分数 25 作者 陈越 单位 浙江大学 An inorder binary tree traversal can be implemented ...

  3. PAT甲级1020 Tree Traversals:[C++题解]树的遍历、由中序序列和后序序列递归建树

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的后序遍历序列和中序遍历序列,让求层次遍历的序列. 分析: 后序遍历:先 左子树.右子树 ,最后再遍历根结点. 中序遍历:先左子树,再根 ...

  4. 1020. Tree Traversals (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1020 题目: 1020. Tree Traversals (25) 时间限制 400 ms 内存 ...

  5. PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历

    文章目录 题目分析 题目链接 题目分析 分析: 给定栈模拟的二叉树的中序序列. 我们可以发现一些性质: 1 第一个值是根结点. 2 对于所有的push操作,如果上一个是push,该结点就是上一个结点的 ...

  6. 03-树3 Tree Traversals Again

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

  7. hdu 1710 Binary Tree Traversals (二叉树)

    1 /********************************************************** 2 题目: Binary Tree Traversals(hdu 1710) ...

  8. 1086 Tree Traversals Again

    1. 这题的核心部分是,根据二叉树的先序序列和中序序列求后序序列.等于是在1020 Tree Traversals这一题的基础上,把怎么得到先序序列和中序序列的难度加大了,不是直接给出,而是要曲折一点 ...

  9. R语言使用caret包构建遗传算法树模型(Tree Models from Genetic Algorithms )构建回归模型、通过method参数指定算法名称

    R语言使用caret包构建遗传算法树模型(Tree Models from Genetic Algorithms  )构建回归模型.通过method参数指定算法名称.通过trainControl函数控 ...

最新文章

  1. oracle文件夹cwallet,Oracle Wallet
  2. 基于Spring的Web缓存
  3. (建议收藏)万字长文,帮你一招搞定产品经理面试-详解产品经理面试大全
  4. 当你不知道今天星期几,不妨在编辑器写下这段代码
  5. 【产品动态】一文详细解读智能数据构建产品Dataphin的“规划”功能
  6. 门禁系统产品选择与施工要点
  7. 自然语言处理工具HanLP被收录中国大数据产业发展的创新技术新书《数据之翼》...
  8. 总结必须学习的10项.NET技术
  9. 计算机网络ppt_计算机网络--女娲补天
  10. C# winform程序运行在XP
  11. 文本聚类分析算法_常用的聚类分析算法
  12. cad批量打印_CAD不懂批量打印的进来 详细讲解一下
  13. 少年派的奇幻漂流-对于日货轮出事故主人公侥幸得生后发生的事情。
  14. 软件架构-nginx详解上
  15. wireshark抓包获取好友ip,定位所在位置
  16. 如何使用EasyNVR+CDN突破萤石云在直播客户端数量上的限制,做到低成本高性价比的直播...
  17. (转) Exploring How Cache Coherency Accelerates Heterogeneous Compute
  18. 【陈工笔记】# 如何用WPS演示给PPT加水印 #
  19. 薪水支付系统领域驱动设计实践
  20. 【算法】二分法求多项式单根

热门文章

  1. 德州扑克游戏算法讲解
  2. mysql 启动和关闭
  3. 常见面试题整理--数据库篇(每位开发者必备
  4. 本科计算机辅导,计算机及应用自考本科辅导
  5. 7步!顶会Paper 复现方法!
  6. 【Web前端】HTML—4.表格标签
  7. 引争议!某高校硕导提议高校教师应多配偶,这样就能多生娃!高校的处理通报来了...
  8. 狼羊菜过河问题c语言算法,算法系列之十四:狼、羊、菜和农夫过河问题
  9. 天天爱跑步noip2016
  10. 复现Dense Extreme Inception Network(pytorch)