1086 Tree Traversals Again——PAT甲级真题
1086 Tree Traversals Again
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.
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
题目大意:用栈来模拟以可二叉树的中序遍历,让你求出这棵二叉树的后序遍历。
大致思路:由题意可知二叉树的输入顺序为二叉树的前序遍历,然后我们借助一个栈来得出二叉树的后续遍历。进行一次Push操作我们就像栈中存一个元素,Pop操作我们就返回栈顶元素并将元素存入数组中。最后得到二叉树的中序遍历序列。
#include <bits/stdc++.h>
using namespace std;
const int N = 65;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int n;
TreeNode root[N];
vector<int> preorder, inorder, postorder; //先序序列,中序序列
void newNode(int x) {
root[x].val = x;
root[x].left = root[x].right = -1;
}
//给出了中序和先序构建二叉树
TreeNode* create(int preL, int preR, int inL, int inR) {
if (preL > preR) return -1;
TreeNode* root = new TreeNode(preorder[preL]);
int k;
for (k = inL; k <= inR; k++) {
if (inorder[k] == preorder[preL]) break;
}
int numL = k - inL;
root->left = create(preL + 1, preL + numL, inL, k - 1);
root->right = create(preL + numL + 1, preR, k + 1, inR);
return root;
}
void postvisit(TreeNode* root) {
if (node == -1) return;
postvisit(root->left);
postvisit(root->right);
postorder.push_back(root->val);
}
int main() {
scanf("%d", &n);
//先将每个节点初始化
stack<int> tmp;
string op;
int id;
while(n--) {
cin >> op;
if (op == "Push") {
cin >> id;
tmp.push(id);
preorder.push_back(id);
}
else {
inorder.push_back(tmp.top());
tmp.pop();
}
}
create(0, n - 1, 0, n - 1);
TreeNode* root = create(0, n - 1, 0 , n - 1);
postvisit(root);
for (int i = 0; i < postorder.size(); i++) {
cout << postorder[i];
if (i != postorder.size() - 1) cout << " ";
else cout << endl;
}
return 0;
}
1086 Tree Traversals Again——PAT甲级真题的更多相关文章
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...
- 1020 Tree Traversals——PAT甲级真题
1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- 1102 Invert a Binary Tree——PAT甲级真题
1102 Invert a Binary Tree The following is from Max Howell @twitter: Google: 90% of our engineers us ...
- PAT甲级真题及训练集
正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...
- PAT 甲级真题题解(121-155)
1121 Damn Single 模拟 // 1121 Damn Single #include <map> #include <vector> #include <cs ...
- PAT甲级真题 A1025 PAT Ranking
题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...
随机推荐
- 搭建Spring相关框架后,配置信息文件头部出现红色小×错误。
问题描述: 在搭建关于Spring相关框架的时候,在applicationContext.xml配置文件和servlet-mvc.xml配件文件的头部会出现一个红色的小X错误: 错误描述: Refer ...
- brew更换国内源
来源:清华大学开源软件镜像站(https://mirror.tuna.tsinghua.edu.cn/help/homebrew/) 替换现有上游 1 # brew 程序本身,Homebrew/Lin ...
- hibernate+spring+tomcat启动报错Not supported by BasicDataSource
最近使用hibernate+spring+jsp的小项目制作过程中出现一些错误,例如: java.lang.UnsupportedOperationException: Not supported b ...
- hadoop知识点总结(三)YARN设计理念及基本架构
YARN设计理念与基本架构 1,MRv1的局限性:扩展性差,可靠性差,资源利用率低,无法支持多种计算框架 2,YARN基本设计思想 1)基本框架对比 Hadoop1.0中,JobTracker由资源管 ...
- vmware安装linux系统,自动建立没选项
虚拟机安装CentOS自己跳过分区,直接就到最后的软件包安装了 建完系统后不用power on,建完后在edit一下系统参数,应该会看见两个cd, 有一个是vmware自己加的,把那个删除后在开机就可 ...
- 关于webservice实现web接口
package service; import java.util.List; import javax.jws.WebMethod;import javax.jws.WebService; /** ...
- vim快捷键收藏版
总述 附加一篇介绍文哈,关于vim快捷键的介绍.vim和vscode 到底谁更好用,大家争得不可开交,然后我就在vscode里面装了一个vim插件,完美得解决了这个问题,用完之后觉得真香,所以我就整理 ...
- Linux lsblk和df命令区别
lsblk 查看的是block device,也就是逻辑磁盘大小. df查看的是file system, 也就是文件系统层的磁盘大小
- Codeforces Round #648 (Div. 2) C. Rotation Matching
题目链接:https://codeforces.com/contest/1365/problem/C 题意 有两个大小为 $n$ 的排列,可以循环左移或右移任意次,问最多有多少对同一值在同一位置. 题 ...
- Codeforces Round #666 (Div. 2) C. Multiples of Length (构造,贪心)
题意:有一个长度为\(n\)的序列,可以操作\(3\)次,每次选取一段区间,然后区间的元素加减区间长度的倍数,\(3\)次操作后使得序列所有元素为\(0\),问具体操作情况. 题解:假如我们能选择一整 ...
