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 ...
随机推荐
- Spark踩坑填坑-聚合函数-序列化异常
Spark踩坑填坑-聚合函数-序列化异常 一.Spark聚合函数特殊场景 二.spark sql group by 三.Spark Caused by: java.io.NotSerializable ...
- .net core 和 WPF 开发升讯威在线客服与营销系统:实现对 IE8 的完全完美支持 【干货】
本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...
- Java常用类库2
1.java.util.Date类 package LESSON9; import java.util.Date; public class demo1 { public static void ma ...
- zookper投票机制
前提:已经搭建好zookper集群 1.先开启编号为01的服务器 2.开启编号为02的服务器,状态为leader,编号为01的变成follower 3.开启编号为03的服务器,状态为follower ...
- UESTC 360(1425) another LCIS
这道题是CD老OJ上面的一道题,现在在新OJ上的题号是360,开始在VJ上做的提交一直RE(囧).后来才知道OJ移位了. 这道题是一个简单的成段更新+区间合并的线段树的题,1A还让我小激动了一下 这道 ...
- poj2778 DNA Sequence(AC自动机+矩阵快速幂)
Description It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's ve ...
- C#程序报找不到时区错误
原因:win10电脑里的时区在win7里不全有 解决:将win10时区注册表导出,在win7电脑上安装 时区注册表路径:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Wi ...
- python自动化运维之CMDB篇-大米哥
python自动化运维之CMDB篇 视频地址:复制这段内容后打开百度网盘手机App,操作更方便哦 链接:https://pan.baidu.com/s/1Oj_sglTi2P1CMjfMkYKwCQ ...
- Nginx 版本回滚
目录 参考信息 源码安装 nginx-1.14.2 版本升级 nginx-1.16.1 版本回滚 ①.对于软件的版本升级.添加官方模块.添加第三方模块,都需要用源码安装包重新生成(configure) ...
- Windows10电脑优化和使用
本文将结合自身经验和短视频软件中的优化技巧,推荐一些Win10系统的优化和使用小技巧. 电脑优化 新电脑调出我的电脑等桌面图标: 右键桌面,选择个性化,左侧选择主题,在相关的设置中找到桌面图标设置,将 ...