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<stack>
#include<cstring>
using namespace std;
const int maxn = ;
struct Node{
int data;
Node* lchild;
Node* rchild;
};
int n,pre[maxn],in[maxn],num = ; Node* createTree(int preL,int preR,int inL,int inR){
if(preL > preR) return NULL;
Node* root = new Node;
root -> data = pre[preL];
//printf("%d\n",root->data);
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){
if(root == NULL) return;
postOrder(root->lchild);
postOrder(root->rchild);
printf("%d",root->data);
num++;
if(num < n) printf(" ");
} int main(){
int x,k1=,k2=;
scanf("%d",&n);
stack<int> st;
char str[];
for(int i = ; i < *n; i++){
scanf("%s",str);
if(strcmp(str,"Push") == ){
scanf("%d",&x);
st.push(x);
pre[k1++] = x;
}else{
in[k2++] = st.top();
st.pop();
}
}
// printf("1\n");
Node* root = createTree(,n-,,n-);
// printf("2\n");
postOrder(root);
return ;
}
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-树3 Tree Traversals Again (25 分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- 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 ...
随机推荐
- Hyperledger Chaincode启动过程
Chaincode 启动过程 简介 这里讲的 Chaincode 是用户链码(User Chaincode,UCC),对应用开发者来说十分重要,它提供了基于区块链分布式账本的状态处理逻辑,基于它可以开 ...
- 简单Factory模式
#pragma once #include "student.h" #include "Teacher.h" typedef enum _EPersonType ...
- list 的扩展
数据挖掘中会遇到添加多个新的特征s,对一个feature = list()来说, 除了可以用 feature.append('xx') # 在尾部添加一个特征 feature.extend(['xx' ...
- Luogu 4949 最短距离
这就是个水题. 一开始想把整个环找出来断开当一条链,然后其他部分正常链剖,两个点之间的路径如果经过环就考虑一下走哪边更快. 但是这样子还是太麻烦了. 我们可以直接断开环上的一条边,然后正常链剖,只要在 ...
- 自定义JTabbedPane 标签风格
自定义JTabbedPane 标签风格 2012年03月09日 20:33:12 阅读数:2435 demo 下载地址:http://download.csdn.net/detail/jinannan ...
- HDU 3723 Delta Wave (高精度+calelan数)
题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答 ...
- easyui 列表 条件检索
onclick="search()" 不要使用search命名检索方法,冲突,无法调用. 通用检索function function searchData() { var objs ...
- datetime 2017-10-21 10:09:02.560 转年月日的时间类型
sql语句时间转年月日格式: 适用于多种时间格式 select REPLACE(STUFF(CONVERT(char(10), REPLACE(CONVERT(varchar(10),'2017-1 ...
- SynchronizationContext应用
这个类的应用,官方的说明并不是很多,主要原因是因为微软又出了一些基于SynchronizationContext的类.比如:BackgroundWorker 大家写程序时经常碰到子线程调用UI线程的方 ...
- Windows 下 MongoDb 简单配置
以管理员的启动cmd 进入安装目录下 输入: mongod --auth --port 3406 --dbpath=库地址 --logp ...