数据结构课后练习题(练习三)7-5 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.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2 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
#include <iostream>
#include <vector>
#include <stack>
#include <cstring> using namespace std; vector<int> pre,in,post,val; void postorder(int root,int start,int end){
if(start>end) return ;
int root_index=;
while(root_index<=end&&in[root_index]!=pre[root]) root_index++;
postorder(root+,start,root_index-);
postorder(root++root_index-start,root_index+,end);
post.push_back(pre[root]);
}
int main()
{
stack<int> sta;
int k=,N;
char ch[];int num;
scanf("%d",&N);
while(~scanf("%s",&ch)){
if(strlen(ch)==) {
scanf("%d",&num);
pre.push_back(num);
sta.push(num);
}else{
in.push_back(sta.top());
sta.pop();
if(in.size()==N) break;
}
}
postorder(,,N-);
for(int i=;i<N;i++)
if(i!=N-) printf("%d ",post[i]);
else printf("%d",post[i]); system("pause");
return ;
}
数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)的更多相关文章
- PTA 03-树3 Tree Traversals Again (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again (25分) An inor ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25 分)(二叉树的遍历)
给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 03-树3 Tree Traversals Again (25 分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历
03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913 An inorde ...
随机推荐
- 编译Android系统
官方网址: http://source.android.com/source/building.html 下面摘录相关内容: Downloading and Building The Android ...
- 获取父窗口iframe的ztree对象
问题如下:我要在jqgrid中获取ztree的选中节点对象 var iframe = parent.$("#ztree的iframeId").contents(); var ztr ...
- 学习笔记:CentOS7学习之二十五:shell中色彩处理和awk使用技巧
目录 学习笔记:CentOS7学习之二十五:shell中色彩处理和awk使用技巧 25.1 Shell中的色彩处理 25.2 awk基本应用 25.2.1 概念 25.2.2实例演示 25.3 awk ...
- 《鸟哥的Linux私房菜:服务器搭建篇》第一部分学习笔记
零.问题集 1. 我们购买网络时,有4M.5M.8M.10M乃至100M,运营商是如何控制每个用户的带宽的? 一.网络基础概念 1. 交换器与集线器最大的差异,在于交换器内有一个特别的内存, 这个内存 ...
- [LuoguP1074]靶形数独_搜索
靶形数独 题目链接:https://www.luogu.org/problem/P1074 数据范围:略. 题解: 传说中的大爆搜题啊. 我觉得这种题就是你能想到什么优化就直接上什么优化.... 这个 ...
- InnoDB 中的锁实现
InnoDB 中的锁实现 原贴:InnoDB 锁系统及死锁检测实现分析 InnoDB 中,所有事务加的行锁通过一个全局的 hash 表 lock_sys 维护: /* The lock system ...
- 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)
--喜欢记得关注我哟[shoshana]-- 目录 1.朴素的模式匹配算法2.KMP模式匹配算法 2.1 KMP模式匹配算法的主体思路 2.2 next[]的定义与求解 2.3 KMP完整代码 2.4 ...
- Photon Server初识(四) --- 部署自己的服务Photon Server
准备工作: 1.一台 window 虚拟机(本机是window也行) 2.下载SDK : https://www.photonengine.com/zh-CN/sdks#server 一:SDK介绍 ...
- 用shell脚本安装MySQL-5.7.22-Percona版本
#!/bin/bash MySQL_Package=Percona-Server-5.7.22-22-Linux.x86_64.ssl101.tar.gz Package_Source=Percona ...
- docker 入门4 - 群 【翻译】
开始,第 4 部分:群 先决条件 安装 Docker 版本 1.13 或更高版本. 获取第 3 部分先决条件中所述的 Docker Compose. 获取 Docker Machine, Mac 的 ...