PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]
题目
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
- 中序+前序建树
- 递归后序遍历树
思路 02(最优)
- 中序+前序直接转后序序列
知识点
- 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) [树的遍历]的更多相关文章
- 1086. Tree Traversals Again (25)-树的遍历
题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- PAT (Advanced Level) 1086. Tree Traversals Again (25)
入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
- 1086. Tree Traversals Again (25)
题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- PAT Advanced 1147 Heaps (30) [堆,树的遍历]
题目 In computer science, a heap is a specialized tree-based data structure that satisfies the heap pr ...
随机推荐
- 如何在ESXi 5.x/6.x(2005205)中下载和安装异步驱动程序
本文提供了在ESXi 5.x和6.x中下载和安装异步驱动程序的步骤 https://kb.vmware.com/s/article/2005205
- pyhton中pandas数据分析模块快速入门(非常容易懂)
//2019.07.16python中pandas模块应用1.pandas是python进行数据分析的数据分析库,它提供了对于大量数据进行分析的函数库和各种方法,它的官网是http://pandas. ...
- 10 MySQL索引选择与使用
索引概述 每种存储引擎对每个表至少支持16个索引,总索引长度至少256字节. MyISAM和InnoDB的表默认创建BTREE索引.MEMORY引擎默认使用HASH索引,但也支持BTR ...
- 小程序 scroll-view 中文字不换行问题
问题描述:在scroll-view 中scroll-x="true"时控制文字超出显示省略号,要求如图: 但实际中会出现如文字不换行或样式错乱的问题. 横向滚动的实现如下: 超过两 ...
- servlet中urlpatterns注意事项
在servlet中, @WebServlet(urlPatterns="/newsAdd")接收 resp.sendRedirect("/wedding/houtai/N ...
- 076-PHP数组修改元素值
<?php $arr=array(98,'hello',67,'A',85,NULL); //定义一个数组 echo '输出数组修改元素之前的详细信息:<br />'; print_ ...
- servlet的基本类和接口
javax.servlet.Servlet接口 javax.servlet.GenericServlet类(协议无关版本) javax.servlet.http.HttpServlet类(HTTP版本 ...
- 三、jsx简化教程
1)使用 JSX 的好处 1.提供更加语意化且易懂的标签 与html对比 <!--HTML写法--> <form class="messageBox"> & ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-lock
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 02-NVIDIA Jetson TX2 通过JetPack 3.1刷机完整版(踩坑版)
未经允许,不得擅自改动和转载 文 | 阿小庆 2018-1-20 本文继第一篇文章:01-NVIDIA Jetson TX2开箱上电显示界面 TX2 出厂时,已经自带了 Ubuntu 16.04 系统 ...