PAT甲级——A1020 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 (≤), 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 7
Sample Output:
4 1 6 3 5 7 2
已知后序遍历和中序遍历输出层序遍历
#include <iostream>
#include <queue>
using namespace std;
int *pos, *ord;//存放后序和中序遍历数据
struct Node
{
int val;
Node *l, *r;
Node(int a = ) :val(a), l(nullptr), r(nullptr) {}
};
Node* createTree(int posL,int posR, int ordL, int ordR)
{
if (posL > posR)
return nullptr;
Node *root = new Node();
root->val = pos[posR];//根节点值
int k;
for (k = ordL; k <= ordR; ++k)
{
if (ord[k] == pos[posR])//找到原树的根
break;
}
int numL = k - ordL;//左子树节点数量
//递归构造左子树
root->l = createTree(posL, posL + numL - , ordL, k - );
//递归构造右子树
root->r = createTree(posL + numL, posR - , k + , ordR);//取出根节点
return root;
}
void getResBFS(Node* root)
{
queue<Node*>q;
Node* p = nullptr;
q.push(root);
cout << root->val;
while (!q.empty())
{
p = q.front();
if (p != root)
cout << " " << p->val;
q.pop();
if (p->l != nullptr)
q.push(p->l);
if (p->r != nullptr)
q.push(p->r);
}
cout << endl;
} int main()
{
int N;
cin >> N;
pos = new int[N];
ord = new int[N];
for (int i = ; i < N; ++i)
cin >> pos[i];
for (int i = ; i < N; ++i)
cin >> ord[i];
Node* root = createTree(, N - , , N - );
getResBFS(root);
return ;
}
PAT甲级——A1020 Tree Traversals的更多相关文章
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- PAT 甲级 1020 Tree Traversals
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...
- PAT甲级——A1086 Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
随机推荐
- skyline中大数据量的三维场景刷新速度问题
我们做了一个的类似于TE Pro的桌面系统来代替TE Pro演示我们的大三维场景.我们的三维场景包括100平方公里的全要素场景,有建筑物,地面.小品.部件.植被等.在系统运行后,三维场景刷不起来,速 ...
- Vue学习笔记——Vue-router
转载:https://blog.csdn.net/guanxiaoyu002/article/details/81116616 第1节:Vue-router入门 .解读router/index.js文 ...
- 转: sizeof,总结
源地址:http://blog.csdn.net/freefalcon/article/details/54839 0. 前向声明 sizeof,一个其貌不扬的家伙,引无数菜鸟竟折腰,小虾我当初也没少 ...
- Sequelize
连接数据库 const DB = require('sequelize') // 连接数据库 const connect = new DB('xjg', 'root', 'root', { host: ...
- Font Awesome 完全兼容 Bootstrap 的所有组件。
"F_FullName": "其他", "F_Icon": "glyphicon glyphicon-backward fa-lg ...
- 安装MySQL出现的this application
1,https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=1385 2,安装Windows 图像组件 (WIC)以及NET Fra ...
- 02-python 学习第二天
今天学习了以下几个方面的内容,虽然部分内容不能理解,跟着老师写出了代码. 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 程序练习1:购物车程序 请闭眼写出以下程序. 程序: ...
- Activit单元i测试(与spring集成测试)
1.测试 eclipse下安装activiti插件以及maven 右键新建activiti project(这时会自动创建pom依赖以及activiti.cfg.xml,但还不是maven项目) 选中 ...
- h5 上下左右前后居中
.outer { width: 200px; height: 200px; background: red; position: relative; } .inner { position: abso ...
- CF627A Xor Equation
题意:a+b=s,a^b=x(异或).问有多少有序Z+对(a,b)满足条件. 标程: #include<cstdio> using namespace std; typedef long ...