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 ...
随机推荐
- 23.NULL 函数
SQL ISNULL().NVL().IFNULL() 和 COALESCE() 函数 请看下面的 "Products" 表: P_Id ProductName UnitPrice ...
- coordinatewise 是什么意思?
As far as I remember, in the context of optimization, "coordinate wise" means that you are ...
- Luogu 4091 [HEOI2016/TJOI2016]求和
BZOJ 4555 一道模板题. 第二类斯特林数有公式: $$S(n, m) = \frac{1}{m!}\sum_{i = 0}^{m}(-1)^i\binom{m}{i}(m - i)^n$$ 考 ...
- Luogu 4069 [SDOI2016]游戏
BZOJ 4515 树链剖分 + 李超线段树 要求支持区间插入一条线段,然后查询一个区间内的最小值.可以使用李超线段树解决,因为要维护一个区间内的最小值,所以每一个结点再维护一个$res$表示这个区间 ...
- Nhibernate HQL 匿名类(严格说是map的使用以及构造函数的使用
1.map的使用 var hql=string.Format(@"select new map( tc.LimitIndexType as LimitIndexType, tc.LimitS ...
- c++基本
要投身游戏业了,自学cocos2d-x之前准备把c++的基础再捡起来 基本语法 1. cout<<"hello world"; 2. 开头写 #include < ...
- 结对编程 四则运算(java)(胡大华 黄绪明)
Github项目地址 https://github.com/yogurt1998/Myapp 项目需求 题目: 实现一个自动生成小学四则运算题目的命令行程序 功能 1.使用-n 参数控制生成题目的个数 ...
- dssfsfsfs
一直都好使啊..好使 好使 MainActivity://overover package com.example.sockettest; import java.io.BufferedReader; ...
- SourceTree使用
SourceTree的基本使用 1. SourceTree是什么 拥有可视化界面的项目版本控制软件,适用于git项目管理 window.mac可用 2. 获取项目代码 1. 点击克隆/新建 2. ...
- lfs原理