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 ...
随机推荐
- eax,ebx,ecx,edx,esi,edi,ebp,esp寄存器的作用
位的寄存器.如果用C语言来解释,可以把这些寄存器当作变量看待. 比方说:add eax,-2 ; //可以认为是给变量eax加上-2这样的一个值. 位寄存器有多种用途,但每一个都有"专长 ...
- VS2010-MFC(常用控件:树形控件Tree Control 下)
转自:http://www.jizhuomi.com/software/203.html 前面一节讲了树形控件Tree Control的简介.通知消息以及相关数据结构,本节继续讲下半部分,包括树形控件 ...
- django2 rest_framework + vue.js + mysql5.6 实现增删改查
1.安装pymysql,mysqlclient,创建项目django-admin startproject django3 2.在Mysql中创建一个数据库叫django3db,打开项目,修改一下数据 ...
- selenium基础(窗口截图)
窗口截图 目的:当脚本执行出错时对当前窗口进行截图 方法:get_screenshot_as_file() #打开百度首页,搜索“selenium",完成后进行截图,并将结果保存至D:/te ...
- java_序列化
import java.io.*; class People implements Serializable { /* * 序列化和反序列化的时候,会抛出就NotSerializableExcepti ...
- open 和 release
我们开始在真实的 scull 函数中使用它们. open 方法 open 方法提供给驱动来做任何的初始化来准备后续的操作. 在大部分驱动中, open 应当 进行下面的工作: 检查设备特定的错误( ...
- cdq分治(偏序)
偏序问题: https://www.luogu.org/blog/Owencodeisking/post-xue-xi-bi-ji-cdq-fen-zhi-hu-zheng-ti-er-fen 优质题 ...
- call和apply的应用
相同点 都能够改变方法的执行上下文(执行环境),将一个对象的方法交给另一个对象来执行,并且是立即执行 var arrayLike = { 0: 'item1', 1: 'item2', 2: 'ite ...
- Spring中的Junit
Spring中的Junit package com.imooc.test.base; import org.junit.After; import org.junit.Before; import o ...
- JavaScript事件(随笔)
1. Javascript事件介绍 JavaScript中的事件和现实生活中的事件类似,现实生活中发生的一些事情,例如:交通事件,当这些事情发生时,我们需要提供处理方案: 在JavaScript中事件 ...