03-树3 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<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = ; struct Node
{
int data;
Node *lchild, *rchild;
}; int in[maxn] = {}, pre[maxn] = {};
int num = ; Node* createTree(int preL, int preR, int inL, int inR);
void postOrder(Node *root,int n); int main()
{
int n;
scanf("%d",&n); int x;
int preIndex = , inIndex = ;
char str[];
stack<int> s; for (int i = ; i < *n; i++)
{
getchar();
scanf("%s",str);
if ( == strcmp(str,"Push"))
{
scanf("%d",&x);
s.push(x);
pre[preIndex++] = x;
}
else
{
x = s.top();
s.pop();
in[inIndex++] = x;
}
} Node *root = createTree(,n-,,n-);
postOrder(root,n);
return ;
} Node* createTree(int preL, int preR, int inL, int inR)
{
if (preL > preR)
{
return NULL;
} Node *root = new Node;
root->data = pre[preL]; int k;
for (k = inL; k <= inR; k++)
{
if (in[k] == pre[preL])
{
break;
}
} int numLeft = k - inL;
root->lchild = createTree(preL+, preL+numLeft, inL, k-);
root->rchild = createTree(preL+numLeft+, preR, k+, inR);
return root;
} void postOrder(Node *root,int n)
{
if (root == NULL)
{
return;
}
postOrder(root->lchild,n);
postOrder(root->rchild,n);
printf("%d",root->data); num++;
if (num < n)
{
printf(" ");
}
}
03-树3 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 ...
- 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)
7-5 Tree Traversals Again (25 分) An inorder binary tree traversal can be implemented in a non-recu ...
- 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 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 ...
- 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 ...
随机推荐
- 换个语言学一下 Golang (10)——并行计算
如果说Go有什么让人一见钟情的特性,那大概就是并行计算了吧. 做个题目 如果我们列出10以下所有能够被3或者5整除的自然数,那么我们得到的是3,5,6和9.这四个数的和是23.那么请计算1000以下( ...
- Nginx中的$document_uri与$request_uri以及$http_referer
Nginx基于$document_uri的访问控制,变量$document_uri该变量等价于$uri,其实也等价于location匹配. 示例1: 当用户请求的url中包含/admin/时,直接返回 ...
- Workerman MySQL组件Connection用法总结
一.初始化连接 $db = new \Workerman\MySQL\Connection('host', 'port', 'user', 'password', 'db_name'); 二.获取所有 ...
- Java 之 LinkedHashSet 集合
一.概述 java.util.LinkedHahset 集合 extends HashSet 集合 在HashSet下面有一个子类java.util.LinkedHashSet,它的底层是一个哈希表( ...
- python bs4 BeautifulSoup
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.bs4 模块的 BeautifulSoup 配合requests库可以写简单的爬虫. 安装 命令:pip in ...
- MYSQL入门这一篇就够了
安装概述 分为5.6与,5.7版本,5.7的安装与5.6略有不同,因为依赖BOOST库,下面给出2个版本的安装脚本,直接运行即可 Mysql 5.6 [root@Tuiliu ~]# cat mysq ...
- Golang Web应用 创建docker镜像笔记(win 平台)
记录的是 本地编译好了再创建容器镜像的方法 ,这样子生成的镜像文件比较小,方便分发部署 win 平台需要设置golang交叉编译 生成linux可执行文件 CMD下: Set GOOS="l ...
- Win10 家庭版 VMware 无法启动 解决办法
引发原因 最近更新了一个补丁 KB4524147 安装后会导致 VM 无法打开(如果你没有安装hyper-v的话) 解决方案 控制面板 -> 程序 -> 查看已安装的更新 -> 找到 ...
- git使用过程中的若干问题笔记
1.关于本地分支创建之后,如何在远程创建同名分支并完成本地分支到远程分支的push 首先创建本地库分支以dev为例 然后输入命令git push --set-upstream origin dev / ...
- 行业——5G
1. 概述 1.1 定义 5G:5th Generation Mobile Networks / 5th Generation Wireless Systems,第5代移动通信技术 1.2 商标 ...