题目如下:

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)的更多相关文章

  1. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  2. 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 ...

  3. 1086. Tree Traversals Again (25)-树的遍历

    题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...

  4. PAT (Advanced Level) 1086. Tree Traversals Again (25)

    入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...

  5. 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

    题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...

  6. pat1086. Tree Traversals Again (25)

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. PAT 1086 Tree Traversals Again[中序转后序][难]

    1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...

  8. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

随机推荐

  1. 2015 多校联赛 ——HDU5353(构造)

    Each soda has some candies in their hand. And they want to make the number of candies the same by do ...

  2. 2015 多校联赛 ——HDU5350(huffman)

    Problem Description MZL is a mysterious mathematician, and he proposed a mysterious function at his ...

  3. [UOJ UR#16]破坏发射台

    来自FallDream的博客,未经允许,请勿转载,谢谢. 传送门 先考虑n是奇数的情况,很容易想到一个dp,f[i][0/1]表示转移到第i个数,第i个数是不是第一个数的方案数,然后用矩阵乘法优化一下 ...

  4. [BZOJ]3243 向量内积(Noi2013)

    小C做了之后很有感觉的题目之一,但因为姿势不对调了很久. Description 两个d 维向量A=[a1,a2,...,ad]与B=[b1,b2,...,bd]的内积为其相对应维度的权值的乘积和,即 ...

  5. CAN通信要注意的问题

    CAN通信要注意的问题主要有: 1.参数配置 在初始化.启动.发送.接收 CAN信息时都要对CAN卡进行参数配置: (1)CAN卡的选择,我用到的是周立功的PCIE-9221,也就是DevType设备 ...

  6. python2.7入门---变量类型

      这篇文章呢,主要是用来记录python中的变量类型学习内容的.接下来就来看一下变量类型,那么什么是变量呢.变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间.基于变量的数据类型,解 ...

  7. Linux系统之TroubleShooting(故障排除)(转)

    尽管Linux系统非常强大,稳定,但是我们在使用过程当中,如果人为操作不当,仍然会影响系统,甚至可能使得系统无法开机,无法运行服务等等各种问题.那么这篇博文就总结一下一些常见的故障排除方法,但是不可能 ...

  8. mybatis下使用log4j打印sql语句和执行结果

    转载自:https://www.cnblogs.com/jeevan/p/3493972.html 本来以为很简单的问题, 结果自己搞了半天还是不行; 然后google, baidu, 搜出来各种方法 ...

  9. 树莓派3B(2)- 配置多个wifi,自动寻找可用网络

    一.背景 在上篇<Raspberry Pi 3B 安装系统并联网>中,树莓派使用wifi连接,但是把树莓派带到公司,树莓派就连不了公司的wifi,要是支持连接多个wifi就好了,在此整理分 ...

  10. canvas初学 半动态画太极图

    可直接复制粘贴运行 <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head&g ...