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 ...
最新文章
- jedis ShardedJedisPool的 HASH一致性算法(一)从String 的hashcode说起
- android纯白背景加灰,Android背景颜色设置为灰色而不是@android:颜色/白色
- PhysicsJoint
- 直播源码中有哪些网络协议需要注意?
- sql java 创建数据库_java动态创建数据库(sql server)
- c语言有趣代码_为什么C语言永远不会过时?
- java 导出bcp文件格式_使用BCP导出导入数据
- MyEclipse下连接Mysql
- python中pep8规范_Python PEP8规范
- java 中的事物怎么配置_java – 在hibernate中如何以编程方式设置事务的隔离级别,或者如何创建具有不同隔离级别的两个事务...
- 支付宝当面付扫码支付支付后不回调_【支付宝支付】支付宝手机网站支付流程...
- 密码学笔记——培根密码
- 上传图片到腾讯云(海外服务器)com.qcloud5.5.4版本
- 新手焊接电路板_手把手教您如何掌握焊接电路板基础知识
- 电信增值短信平台模块清单(100万级)
- Java实现神经网络方法
- 全能终端神器mobaxterm入坑指南
- python中文编辑_python用Tkinter做自己的中文代码编辑器
- (文末福利)如果代码莫名其妙跑起来了,就不要去动它了……吗?
- monthly rollup和security only的区别
热门文章
- FoodDelivered-Robot---送餐机器人(六)模块驱动代码---IO采集部分
- [解决问题]Android Studio报错:some kotlin libraries attached to this project were compiled with a newer kot
- 【JPress】Menu
- 2014中国高中排行榜发布 华中师大一附中居首
- msm8996平台的一些debug方法
- VSCode已经设置过为中文但变成英文的解决办法
- vuex中的actions
- 操作系统的功能是提高计算机的运行速度吗,Win10系统提高电脑运行速度的具体操作方法...
- 怎么做好备件管理?备件管理系统都包括哪些功能模块?
- python_分水岭算法