1086 Tree Traversals Again (25分)
1 题目
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
PopSample Output:
3 4 2 6 5 1
2 解析
- 题意:根据栈的出栈、入栈序列,输出二叉树的后序序列
- 思路:
- 入栈序列为先序序列,出栈序列为中序序列
- 1 根据先序和中序序列建立二叉树,然后后序遍历输出二叉树
3 参考代码
#include <cstdio>
#include <iostream>
#include <stack>
#include <string>using std::cin;
using std::stack;
using std::string;const int MAXN = 35;
int in[MAXN];
int pre[MAXN];
int N;struct node
{int data;node* lchild;node* rchild;
};node* Create(int preL, int preR, int inL, int inR){if(preL > preR){return NULL;}node* root = new node;root->data = pre[preL];int k;for (k = inL; k <= inR; ++k){if(in[k] == root->data){break;}}int numLeft = k - inL;root->lchild = Create(preL + 1, preL + numLeft, inL, k - 1);root->rchild = Create(preL + numLeft + 1, preR, k + 1, inR);return root;
}int num = 0;
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 argc, char const *argv[])
{scanf("%d", &N);string str;int temp, num,inCount = 0,preCount = 0;stack<int> s;for (int i = 0; i < 2 * N; ++i){cin >> str;if(str == "Push"){scanf("%d", &num);s.push(num);pre[preCount++] = num;}else{temp = s.top();s.pop();in[inCount++] = temp;}}node* root = Create(0, N - 1, 0, N - 1);postorder(root);return 0;
}
1086 Tree Traversals Again (25分)相关推荐
- 【PAT (Advanced Level) Practice】1086 Tree Traversals Again (25 分)
众所周知只有前序遍历和后序遍历是不可能推出中序遍历的,所以我就不废话了 由树的前序遍历和中序遍历可以推后序遍历 由树的中序遍历和后序遍历可以推前序遍历 #include <cstdio> ...
- 1086 Tree Traversals Again (25 分)【一般 / 建树 树的遍历】
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 首先要知道的是: 入栈顺序就是树的前序遍历的 ...
- 03-树3 Tree Traversals Again (25 分)
题目描述: C代码实现及详解(注释部分): #include<stdio.h> #include<stdlib.h> #include<string.h> #def ...
- PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历
文章目录 题目分析 题目链接 题目分析 分析: 给定栈模拟的二叉树的中序序列. 我们可以发现一些性质: 1 第一个值是根结点. 2 对于所有的push操作,如果上一个是push,该结点就是上一个结点的 ...
- pat03-树3. Tree Traversals Again (25)
03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- 1086 Tree Traversals Again
1. 这题的核心部分是,根据二叉树的先序序列和中序序列求后序序列.等于是在1020 Tree Traversals这一题的基础上,把怎么得到先序序列和中序序列的难度加大了,不是直接给出,而是要曲折一点 ...
- 1020. Tree Traversals (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1020 题目: 1020. Tree Traversals (25) 时间限制 400 ms 内存 ...
- PAT甲级1066 Root of AVL Tree (25分):[C++题解]建立平衡树(AVL树)
文章目录 题目分析 题目链接 题目分析 图片来源:acwing 分析 平衡树(AVL树)是平衡二叉搜索树的简称,当然需要满足二叉搜索树的性质,左子树小于根,根小于等于右子树:然后还要满足平衡树的基本特 ...
- C++学习之路 | PTA(甲级)—— 1043 Is It a Binary Search Tree (25分)(带注释)(精简)
1043 Is It a Binary Search Tree (25分) A Binary Search Tree (BST) is recursively defined as a binary ...
最新文章
- JAVA图形界面(GUI)之布局管理器
- 【SSM框架系列】Spring-MVC的组件解析
- qt定时连续发送udp数据包_TCP和UDP
- 关于迷笛音乐节的重大通知-_-
- SpringBoot笔记:SpringBoot集成SpringbootAdmin监控
- Django报错:ConnectionAbortedError: [WinError 10053] 你的主机中的软件中止了一个已建立的连接。...
- Entity Framework在WCF中序列化的问题(转)
- Building designing UVA - 11039
- r语言代码html,R语言学习笔记-内附实例及代码
- macOS设备上的照片导入Bridge
- Fiddler4 抓取Chrome浏览器的Http(s)
- 使用ESP8266/ESP8285做一个WIFI中继(WiFi信号放大器)
- 中国人口总数、老龄人口占比及2040人口年龄结构变化预测
- 百度seo引流怎么做?一个免费分享知识的平台
- java和android!怒斩获了30家互联网公司offer,分享PDF高清版
- Visio里Mathtype公式变形问题解决
- java 采用MD5加密解密代码示例(不玩套路, 非标题党, 附带解密代码)
- Liquibase中利用changelog增加表字段
- 本关任务:编写一个Point类,有x、y两个属性。编写一个PointDemo类,并提供一个distance(Point p1,Point p2)方法用于计算两点之间的距离,实例化两个具体的Point对
- 如何是音乐再ios展台