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. Redis06——Redis到底能用在什么地方(上)

    之前我们介绍了一些列关于Redis的数据结构.持久化.过期&淘汰策略.集群化等知识点,感兴趣的小伙伴可以在文章的末尾查看往期内容.今天将为大家带来Redis的应用.由于本篇文章较长,所以将拆分 ...

  2. ggplot2(2) 从qplot开始入门

    2.1 简介 qplot的意思是快速作图(quick plot). qplot是一种快捷方式,如果您已习惯于使用基础plot(),则可以使用它.它可以使用一致的调用模式快速创建许多不同类型的图. qp ...

  3. python 异步请求

    这是循环请求10次页面.总时间大概是10秒左右,如果是普通的循环请求10次页面而不添加异步的话,时间大概在30秒以上,当然这个数据可能有误,因为有网速的问题存在,但大体的效果应该是不变的. impor ...

  4. C++ 理解类 和 类中的public、protected、private

    我们要明确,不只是C++有类,很多语言也会用到类,因为现在很多都是面向对象编程... 在c++中,关于类的理解,个人理解是这样的,具有共同属性的一个集合被称为类, 比如说人这个集合,具有性别,年龄,出 ...

  5. (转)GNU风格ARM汇编语法指南(非常详细)1

    原文地址:http://zqwt.012.blog.163.com/blog/static/120446842010111481551809/ 汇编源程序一般用于系统最基本的初始化:初始化堆栈指针.设 ...

  6. js Number方法总结

    Number构造属性 Number.EPSILON // 两个可表示(representable)数之间的最小间隔. Number.MAX_SAFE_INTEGER // JavaScript 中最大 ...

  7. Ext.grid rowexpander的展开与收缩

    这里写Ext.grid.Panel的展开与收缩. 1. 确保在grid存在rowexpander对象: plugins: [{ ptype: 'rowexpander', rowBodyTpl: [' ...

  8. Asp.net textbox 控件 的 onTextChange 事件

    <asp:TextBox ID="txt_MailType" runat="server" OnTextChanged="exitMailTyp ...

  9. linux下查看系统版本

    工作中我们会遇到安装软件需要知道linux是什么发行版本,话不多话上干货(按照我认为常用排序) 1. lsb_release -a 名词解释:LSB (Linux Standard Base) # 如 ...

  10. 面试话痨(二)C:JAVA String,别以为你穿个马甲我就不认识你了

    面试话痨系列是从技术广度的角度去回答面试官提的问题,适合萌新观看!   面试官,别再问我火箭怎么造了,我知道螺丝的四种拧法,你想听吗? String相关的题目,是面试中经常考察的点,当面试中遇到了St ...