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 ...
随机推荐
- keystone部署及操作
目录 一 版本信息 二 部署keystone 三 keystone操作 四 验证 五 创建脚本 六 keystone使用套路总结 一.版本信息 官网http://docs.openstac ...
- 使用mail架包发送邮件javax.mail.AuthenticationFailedException: failed to connect at javax.mail.Service.connec
这个错误是因为连接不上邮箱服务器导致的,可能有以下几个原因(以网易邮箱为例) 1.当使用第三方登录邮箱时需要有邮箱的授权码,且要开启POP3/SMTP/IMAP:服务 2.在代码中要调用网易邮箱的密码 ...
- TreeView的绑定与读取
/// <summary> /// 绑定TreeView /// </summary> public void BindTreeVie ...
- zend studio永久使用的方法
安装时选择试用版,以后每天的剩余天数会减少,找到c盘->用户->administrator删除三个文件(.zend,.zend studio,.zs)即可,.zs往往是隐藏的,这时需要选择 ...
- Requests接口测试(二)
requests安装先看下怎么安装requests, 执行以下命令: pip install requests 安装好后如何导入requests模块呢? 如下所示: import requests 基 ...
- struts2 、mybatis 、easyui 分页
rows page 控件自动提交这两个参数 pageSize number The page size. 10pageNumber number Show the page number when p ...
- c# 半角转换为全角 判断是否是全角
#region 半角转换为全角 /// <summary> /// 半角转换为全角 ////转全角的函数(SBC case) ///任意字符串 ///全角空格为12288,半角空格为32 ...
- ECS服务里或者阿里云服务器的二级域名设置方法
我们要实现的效果是,xuxinshuai.abc.com ,具体怎么实现,看下面的流程 第一步:备案域名要有,假如就是www.abc.com 第二步:网站的服务器是IIS的情况下,在部署网站时,需要设 ...
- 如何优雅地使用win10的Linux子系统
转自: http://blog.csdn.net/u010053050/article/details/52388663 http://www.rehack.cn/techshare/devtools ...
- kafka启动报错Cannot allocate memory;There is insufficient memory for the Java Runtime Environment to continue.
kafka启动过程报错,配置没有问题,这就懵了!! Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000 ...