1020 Tree Traversals——PAT甲级真题
1020 Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7Sample Output:
4 1 6 3 5 7 2
题目大意:给你一颗二叉树的后序遍历序列和中序遍历序列。然后求出这颗二叉树的层次遍历序列。
大致思路:首先我们因该知道后序遍历序列的最后一个点为二叉树的根节点,在中序遍历序列中根节点左侧都为左子树,右侧都为右子树。所以我们要首先找到二叉树的根节点,然后在中序遍历中找到根节点所在的位置,然后对二叉树的左右子树递归的调用上述过程。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 35;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int postorder[N], inorder[N]; //后序遍历数组,中序遍历数组
int preorder[N];
int n;
//由后续和中序遍历创建二叉树
TreeNode* buildTree(int postL, int postR, int inL, int inR) {
if (postL > postR)
return NULL; //设置递归返回条件,当postL ==
//postR时表明指向叶子节点,当postl > postR时,应当返回
TreeNode* node = new TreeNode(postorder[postR]);
//查找根节点在中序遍历中的位置
int k;
for (k = inL; k <= inR; k++) {
if (inorder[k] == postorder[postR]) break;
}
int numL = k - inL; //计算左子树结点的个数
//中序遍历中根节点左边的是左子树,右边的是右子树递归建树
node->left = buildTree(postL, postL + numL - 1, inL, k - 1);
node->right = buildTree(postL + numL, postR - 1, k + 1, inR);
return node;
}
//由前序和中序遍历创建二叉树
TreeNode* buildTree2(int preL, int preR, int inL, int inR) {
if (preL > preR) return nullptr;
TreeNode *node = new TreeNode(preorder[preL]);
int k;
for (k = inL; k <= inR; k++) {
if (inorder[k] == preorder[preL]) break;
}
int numL = k - inL;
node->left = buildTree2(preL + 1, preL + numL, inL, k - 1);
node->right = buildTree2(preL + numL + 1, preR, k + 1, inR);
return node;
}
//先序遍历
void inordervisit(TreeNode* root) {
if (root == nullptr) return;
cout << root->val << " ";
inordervisit(root->left);
inordervisit(root->right);
}
//层次遍历
void BFS(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int cnt++;
while(!q.empty()) {
auto node = q.front(); q.pop();
cout << node->val;
cnt++;
if (cnt != n) cout << " ";
else cout << endl;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &postorder[i]);
for (int i = 1; i <= n; i++) scanf("%d", &inorder[i]);
TreeNode* root = buildTree(1, n, 1, n);
BFS(root);
// inordervisit(root);
return 0;
}
1020 Tree Traversals——PAT甲级真题的更多相关文章
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- PAT 甲级真题
1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...
- 1086 Tree Traversals Again——PAT甲级真题
1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...
- PAT 甲级真题题解(63-120)
2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在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 ...
随机推荐
- Java 性能调优的 11 个实用技巧
大多数开发人员认为性能优化是个比较复杂的问题,需要大量的经验和知识.是的,这并不没有错.诚然,优化应用程序以获得最好的性能并不是一件容易的事情,但这并不意味着你在没有获得这些经验和知识之前就不能做任何 ...
- python----类,面向对象(封装、继承、多态)(属性,方法)
什么是对象? 对象是内存中专门用来存储数据的一块区域 对象中可以存放各种数据(数字.代码等) 对象由三部分组成(1,对象标识(id)2,对象类型(type)3,对象的值(value)) 面向对象编程是 ...
- VXLAN配置实例(华为)
常用命令总结: bridge-domain bd-id,创建广播域BD,并进入BD视图. description description,配置BD的描述信息. l2 binding vlan vlan ...
- 闲聊CAP、BASE与XA
CAP理论与BASE理论 首先要和大家说的就是大名鼎鼎的CAP理论与BASE理论了,这两个理论与解决分布式事务问题是密切相关的. 其实网上有很多关于CAP与BASE相关的文章,一写就写了一大堆,篇幅很 ...
- kafka背着你做了什么?
Kafka中有broker.主题.分区.副本等概念,底层有日志和日志分片. 我们先简单介绍一下这些概念,做个类比. broker可以简单理解为一台物理机,其实一台机器上可以有多个broker进程,但是 ...
- Luogu P2408 不同子串个数【SAM】
P2408 不同子串个数 计算一个字符串的不同子串个数 两种方法,一种是\(dp\)出来\(SAM\)从起点开始的路径数量 另一种方法就是计算每个点的\(len[i]-len[link[i]]\)这个 ...
- gym101002K. Inversions (FFT)
题意:给定一个仅含有AB的字母串 如果i有一个B j有一个A 且j>i 会对F(j-i)产生贡献 求出所有发Fi 题解:好像是很裸的FFT B的分布可以看作一个多项式 同理A也可以 然后把B的位 ...
- 【noi 2.6_9277】Logs Stacking堆木头(DP)
题意:给出在最底层的木头的个数,问有多少种堆放木头的方式.要求木头必须互相挨着在一起. 解法:f[i]表示最底层i个木头的堆放木头的方式.注意递推的思想!只需知道上一层堆放0~i-1个(即最底层堆放i ...
- 【noi 2.6_9265】取数游戏(DP)
题意:从自然数1到N中不取相邻2数地取走任意个数,问方案数. 解法:f[i][1]表示在前i个数中选了第i个的方案数,f[i][0]表示没有选第i个.f[i][1]=f[i-1][0]; f[i][ ...
- Ubuntu中Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend)问题的解决
参考博客:https://blog.csdn.net/shimadear/article/details/90598646 问题描述: 解决方法: 第一种情况: 进程中存在与apt相关的正在运行的进程 ...