题目

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

#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<int> pre,in;
int index,n;
struct node {
int data;
node * left;
node * right;
};
node * create(int preL,int preR,int inL,int inR) {
if(preL>preR)return NULL;
node * now = new node;
now->data=pre[preL];
int k=inL;
while(k<inR&&in[k]!=pre[preL])k++;
now->left=create(preL+1, preL+(k-inL), inL, k-1);
now->right=create(preL+(k-inL)+1, preR, k+1, inR);
return now;
}
void postOrder(node * root) {
if(root==NULL)return;
postOrder(root->left);
postOrder(root->right);
printf("%d",root->data);
if(++index<n)printf(" ");
}
int main(int argc,char * argv[]) {
int id;
string s;
stack<int> sc;
scanf("%d",&n);
int len=n<<1;
for(int i=0; i<len; i++) {
cin>>s;
if(s=="Push") {
scanf("%d",&id);
pre.push_back(id);//preorder
sc.push(id);
}
if(s=="Pop") {
in.push_back(sc.top());
sc.pop();
}
}
node * root = create(0,n-1,0,n-1);
postOrder(root);
return 0;
}

Code 02(最优)

#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
using namespace std;
vector<int> pre,in,post;
int n;
void postOrder(int preL,int preR,int inL,int inR){
if(inL>inR)return;// preL>preR也可以
int k=inL;
while(k<inR&&in[k]!=pre[preL])k++;
postOrder(preL+1, preL+(k-inL), inL, k-1);//先存放左子节点
postOrder(preL+(k-inL)+1, preR, k+1, inR);//后存放右子节点
post.push_back(pre[preL]); //再存放父节点
}
int main(int argc,char * argv[]) {
int id;
char s[5];
stack<int> sc;
scanf("%d",&n);
while(~scanf("%s",s)) { //等价于scanf("%s",s)!=EOF
if(strlen(s)==4) {
//Push
scanf("%d",&id);
pre.push_back(id);
sc.push(id);
} else {
//Pop
in.push_back(sc.top());
sc.pop();
}
}
postOrder(0,n-1,0,n-1);
for(int i=0;i<post.size();i++){
printf("%d",post[i]);
if(i!=post.size()-1)printf(" ");
}
return 0;
}

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. Windows安装tensorflow,配置vs2013,anaconda3.4,cudn9.0,cudnn7.0和pycharm

    前言 最近要开始学习深度,那么首先在电脑上安装tensorflow.但是我不知道是配置版本的问题,还是安装失误的问题,我安装了很久没有安装成功,最后重装了电脑,并且融合了所有的网上可以查到的方案才安装 ...

  2. Shell脚本之awk篇

    目录:一.概述二.awk基本语法格式三.awk基本操作四.awk条件及循环语句五.awk函数六.awk演示示例(源自于man手册) 一.概述 1. 产品概述: awk是一种编程语言,用于在linux/ ...

  3. 关于连接查询主要是左右连接查询中,where和on的区别

    工作中,今天用到左连接查询,我自己造的数据,需要根据条件进行筛选,但是筛选不符合我的要求,最终发现是左右连接中where和on的区别,在作怪,工作中用的表关联太多,我下面简化要点,仅仅把注意点写个简单 ...

  4. 移动端触屏click点击事件延迟问题,以及tap的解决方案

    在移动端 触屏click事件虽然也会响应,但是总感觉是有延迟,一直听说click事件在手机上有200~300毫秒的延迟问题,亲自测了一下,在pc端模拟手机的话是测不出来的,但是用手机测试时发现延迟非常 ...

  5. 实验吧-杂项-MD5之守株待兔(时间戳&python时间戳函数time.time())

    其实也有点蒙圈,因为从没做过和时间戳有关的题. 打开网站,将系统密钥解密得到一串值,而自己的密钥解密是空的,既然说是要和系统匹配,就把解密得到的值以get方式送出去. 但是发现还是在自己的密钥也发生了 ...

  6. hdu 1277 全文检索 (字典树应用)

    全文检索 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. Nmap目录扫描和漏洞扫描(9.27 第十三天)

    目录扫描:扫描站点的目录,寻找敏感文件(目录名.探针文件.后台.robots.txt[].备份文件等) 目录:站点结构,权限控制不严格 探针文件:服务器配置信息,phpinfo.php   readm ...

  8. C++ 设置自动启动

    WCHAR pFileName[MAX_PATH] = {}; //得到程序自身的全路径 DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PA ...

  9. python脚本文件引用

    二.Python __init__.py 作用详解 https://www.cnblogs.com/Lands-ljk/p/5880483.html __init__.py 文件的作用是将文件夹变为一 ...

  10. bfs--P1301 魔鬼之城

    *传送 求最小步数,bfs求解.因为题目要求可以走八个方向(上下左右和对角线),所以两个方位数组来找八个方向 int dirx[9]={0,0,1,1,1,0,-1,-1,-1}; int diry[ ...