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的更多相关文章

  1. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  2. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  3. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  4. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. PAT 甲级 1086 Tree Traversals Again

    https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...

  6. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

  7. PAT甲级——A1086 Tree Traversals Again

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  8. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  9. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

随机推荐

  1. Socket.EndReceive 方法 (IAsyncResult)

        .NET Framework (current version)   其他版本 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 . ...

  2. js 定位到某个锚点的方法

    html页面内可以设置锚点,锚点定义 Html代码  ? 1 <a name="firstAnchor">&nsbp;</a> 锚点使用 Html代 ...

  3. JS 变量的数据类型 运算符

    JS中变量的类型有:数值型.字符型.布尔型.undefined.null.array.object.function 1.数值型:可以进行算术运算的(加.减.乘.除) 数值型包括:整型(整数)和浮点型 ...

  4. 如何查找一个命令由哪个rpm安装&&rpm 的相关查询方法

    [root@test-can-nginx src]# which python3 /usr/bin/python3 [root@test-can-nginx src]# rpm -qf /usr/bi ...

  5. PAT甲级——A1133 Splitting A Linked List【25】

    Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...

  6. openstack实战部署

    简介:Openstack系统是由几个关键服务组成,他们可以单独安装,这些服务根据你的云需求工作在一起,这些服务包括计算服务.认证服务.网络服务.镜像服务.块存储服务.对象存储服务.计量服务.编排服务和 ...

  7. [PKUSC2018]神仙的游戏

    题目 画一画就会发现一些奇诡的性质 首先如果\(len\)为一个\(\operatorname{border}\),那么必然对于\(\forall i\in [1,len]\),都会有\(s_i=s_ ...

  8. 批量处理数据 SqlBulkCopy

    string connectionString = new PublicDBHelper().GetCon(System.Configuration.ConfigurationManager.AppS ...

  9. Activiti业务键(businessKey)

    问题:如何让业务对象和对应的流程 关联? 发现ProcessInstance 有个方法getBusinessKey()可以得到一个businessKey. ProcessInstance 对应数据库中 ...

  10. spark jdk8 单词统计示例

    在github上有spark-java8 实例地址: https://github.com/ypriverol/spark-java8 https://github.com/ihr/java8-spa ...