1086. 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 (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N 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
题目要求通过栈操作来建树,看似题目只给出了中序遍历的结果(出栈顺序),其实仔细观察可以发现压栈顺序就是先序遍历,因此题目给出了先序序列和中序序列,使用中序序列和任一其他序列就可以重新建立树,原理在于,通过先序从前到后处理或者后序从后到前处理都可以得到根结点的编号,通过这个编号,在中序序列中找到后,左侧就是左子树、右侧就是右子树。
本题给出了先序序列和中序序列,因此从前到后遍历先序序列,每次从中序序列中找到左子树和右子树范围,用left和right标记,进行递归,如果left!=right,说明当前结点不是叶子结点,继续递归左右子树,如果left=right,说明子树长度为1,也就是叶子结点。
需要注意的是,读取带空格的行需要getline,如果cin和getline混用,会使得getline多读一个空行,因此全部使用getline读取,使用stringstream实现字符串和数字的转换。
判断命令时,只需要判断第二个字符,如果第二个字符是u,说明是Push操作,从第5个位置开始截串就可以拿到数字。
代码如下:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <sstream>
#include <stack> using namespace std; int N,cur;
vector<int> preOrder;
vector<int> inOrder; typedef struct TreeNode *Node;
struct TreeNode{
int num;
Node left,right; TreeNode(){
left = NULL;
right = NULL;
} }; int findRootIndex(int rootNum){ for(int i = 0;i < N; i++){
if(inOrder[i] == rootNum){
return i;
}
}
return -1; } Node CreateTree(int left, int right){
if(left > right) return NULL;
int root = preOrder[cur];
cur++;
int rootIndex = findRootIndex(root);
Node T = new TreeNode();
T->num = root;
if(left != right){
T->left = CreateTree(left,rootIndex-1);
T->right = CreateTree(rootIndex+1,right);
}
return T; } bool firstOutPut = true;
void PostOrder(Node T){
if(!T) return;
PostOrder(T->left);
PostOrder(T->right);
if(firstOutPut){
printf("%d",T->num);
firstOutPut = false;
}else{
printf(" %d",T->num);
}
} int main()
{
stringstream ss;
string Nstr;
getline(cin,Nstr);
ss << Nstr;
ss >> N;
ss.clear();
string input;
stack<int> stk;
int value;
for(int i = 0; i < N * 2; i++){
getline(cin,input);
if(input[1] == 'u'){
string num = input.substr(5);
ss << num;
ss >> value;
ss.clear();
stk.push(value);
preOrder.push_back(value);
}else{
value = stk.top();
stk.pop();
inOrder.push_back(value);
}
}
Node T = CreateTree(0,N-1);
PostOrder(T);
return 0;
}
1086. Tree Traversals Again (25)的更多相关文章
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]
题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...
- 1086. Tree Traversals Again (25)-树的遍历
题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...
- PAT (Advanced Level) 1086. Tree Traversals Again (25)
入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
- pat1086. Tree Traversals Again (25)
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1086 Tree Traversals Again[中序转后序][难]
1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1086 Tree Traversals Again
PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...
随机推荐
- 关于非旋转treap的学习
非旋转treap的操作基于split和merge操作,其余操作和普通平衡树一样,复杂度保证方式与旋转treap差不多,都是基于一个随机的参数,这样构出的树树高为\(logn\) split 作用:将原 ...
- Linux 下查看tomcat版本
1.echo $JAVA_HOME 2.进入jdk路径 3.java -version
- 有些时候会看到url参数上出现%BF之类
这是URLDecoder和URLEncoder的原因 因为他们是参数,避免影响网页的连接跳转,再到了服务器的时候会自动转过来 当URL地址中仅包含普通非中文字符串和application/x-www- ...
- 吴恩达深度学习第2课第3周编程作业 的坑(Tensorflow+Tutorial)
可能因为Andrew Ng用的是python3,而我是python2.7的缘故,我发现了坑.如下: 在辅助文件tf_utils.py中的random_mini_batches(X, Y, mini_b ...
- 605. Can Place Flowers
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...
- 566. Reshape the Matrix
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...
- 【python标准库模块五】Xml模块学习
Xml模块 xml本身是一种格式规范,是一种包含了数据以及数据说明的文本格式规范.在json没有兴起之前各行各业进行数据交换的时候用的就是这个.目前在金融行业也在广泛在运用. 举个简单的例子,xml是 ...
- python笔记六(函数的参数、返回值)
一 调用函数 在写函数之前,我们先尝试调用现有的函数 >>> abs(-9) 9 除此之外,还有我们之前使用的len()等.可以用于数据类型转换的 int() float() str ...
- Linux中MySQL忽略表中字段大小写
linux 下,mysql 的表面默认是区分大小写的,windows 下默认不区分大小写,我们大多数在windows 下开发,之后迁移到linux(特别是带有Hibernate的工程),可以修改配置是 ...
- postgresql跨服务器复制数据库
假设名为dbname数据库需要从A服务器拷贝到B服务器 接收服务器B postgres用户 需先重置B服务器postgres系统用户的密码,使之与数据库用户postgres一致: passwd -d ...