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
分析:根据后序遍历和中序遍历 输出层序遍历
我的做法是利用后序遍历和中序遍历建树 再层序遍历输出
利用两个变量来记录我们需要建子树的范围 另一个变量记录每层的根节点
 #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef struct BtNode* Bt;
struct BtNode
{
int Num;
Bt RT;
Bt LT;
};
vector<vector<int> > Order();
Bt BuildTree(Bt T,int i2,int j2,int j1)
{
if (i2 >= j2)
return T;
int i = i2;
for (; i < j2; i++)
{
if (Order[][i] == Order[][j1 - ])
break;
}
if (!T)
{
T = new BtNode();
T->LT = NULL;
T->RT = NULL;
T->Num = Order[][i];
}
T->LT =BuildTree(T->LT,i2,i,j1-j2+i);
T->RT = BuildTree(T->RT,i+,j2,j1-);
return T;
}
int main()
{
int N;
cin >> N;
int num;
for(int i=;i<;i++)
for (int j = ; j < N; j++)
{
cin >> num;
Order[i].push_back(num);
}
Bt T=NULL;
T = BuildTree(T,,N,N);
queue<Bt> Q;
Q.push(T);
cout << T->Num;
while (!Q.empty())
{
if (Q.front()->LT)
{
Q.push(Q.front()->LT);
cout << " " << Q.front()->LT->Num;
}
if (Q.front()->RT)
{
Q.push(Q.front()->RT);
cout << " " << Q.front()->RT->Num;
}
Q.pop();
}
return ;
}

1020 Tree Traversals (25 分)的更多相关文章

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

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

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

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

  3. PAT Advanced 1020 Tree Traversals (25 分)

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

  4. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  5. 1020 Tree Traversals (25分)思路分析 + 满分代码

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

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

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

  7. 1020. Tree Traversals (25)

    the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...

  8. 1020. Tree Traversals (25) -BFS

    题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  9. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

随机推荐

  1. 02 layui 下载和搭建环境

    Layui官方网站 官方网站:https://www.layui.com/ 下载地址:https://res.layui.com/static/download/layui/layui-v2.5.5. ...

  2. 结题报告--P5551洛谷--Chino的树学

    题目:点此 题目描述 Chino树是一棵具有某种性质的满二叉树,具体来说,对于这棵树的每一个非叶子节点,它的左子节点(A)(A)(A)的右子节点(C)(C)(C)与它的右子节点(B)(B)(B)的左子 ...

  3. http2 技术整理 nginx 搭建 http2 wireshark 抓包分析 server push 服务端推送

    使用 nginx 搭建一个 http2 的站点,准备所需: 1,域名 .com .net 均可(国内域名需要 icp 备案) 2,云主机一个,可以自由的安装配置软件的服务器 3,https 证书 ht ...

  4. Oracle学习笔记--Oracle启动过程归纳整理

    Oracle 启动过程分为nomount状态mount状态open状态 每个状态下Oracle都会进行不同的操作:1.nomount状态 在$ORACLE_HOME/dbs目录下寻找参数文件 参数文件 ...

  5. 《JavaScript 模式》读书笔记(2)— 基本技巧1

    这篇文章的主要内容,介绍了一些js编程中的基本技巧,其实这些技巧,大家在开发的过程中,或多或少都在使用,或者已经可以熟练的应用于自己的代码或项目中了.那么,这篇文章,就一起来回顾下这些“基本技巧”. ...

  6. Asp.Net Core 2.0实现HttpResponse中繁切换

    随笔背景:因为项目中有个简单的功能是需要实现中文简体到繁体的切换,数据库中存储的源数据都是中文简体的,为了省事就想着通过HttpHeader的方式来控制Api返回对应的繁体数据. 实现方式:通过Asp ...

  7. 初识JVM:(一)JVM工作原理和流程

    本文主要参考:http://blog.csdn.net/CSDN_980979768/article/details/47281037?locationNum=7&fps=1 声明:主要用于个 ...

  8. git删除已经提交的包含敏感信息的文件(还没提交到远程仓库)

    写好的代码已经提交了(但还没push到github),发现某个文件里包含密码.如果push的话,密码可就被公开了.如果在代码里改掉密码,再commit一次,也不行,历史提交记录还是会上传到github ...

  9. JetBrains Quest 3

    起因 今天早上看了一篇博文: 此时的我:"哎,这等好事不等我!" 然后......我打开官方推特: "什么?最后的任务?" 于是就有了这篇文章 开始操作 1.神 ...

  10. Minio 集群扩容存储空间,配合nginx 负载反向代理后端minio 集群服务器,提升高可用性

    环境:Centos  7 软件:minio,Etcd 需求:通过联盟两个集群实例,实现水平扩容存储空间问题: 服务器使用阿里云,一共4台服务器(官方说明最好4台服务器做分布式,测试节省服务器所以我们使 ...