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 (<=30), 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<cstdio>
#include<iostream>
#include<queue>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data;
}node;
int in[], post[];
int N;
node* create(int inL, int inR, int postL, int postR){
if(postL > postR){
return NULL;
}
int head = postR;
node * root = new node;
root->data = post[head];
int i;
for(i = inL; i <= inR; i++){
if(in[i] == post[head])
break;
}
int numL = i - inL;
root->lchild = create(inL, i - , postL, postL + numL - );
root->rchild = create(i + , inR, postL + numL, postR - );
return root;
}
void levelOrder(node* tree){
queue<node*> Q;
Q.push(tree);
node* temp;
int cnt = ;
while(Q.empty() == false){
temp = Q.front();
cnt++;
Q.pop();
printf("%d", temp->data);
if(cnt != N)
printf(" ");
if(temp->lchild != NULL)
Q.push(temp->lchild);
if(temp->rchild != NULL)
Q.push(temp->rchild);
}
}
int main(){
scanf("%d", &N);
for(int i = ; i < N; i++)
scanf("%d", &post[i]);
for(int i = ; i < N; i++)
scanf("%d", &in[i]);
node* tree = create(, N - , , N - );
levelOrder(tree);
cin >> N;
return ;
}

总结:

1、题意:给出后序遍历和中序遍历,求层序遍历。

2、层序遍历时不要忘记在访问完元素后pop。

3、创建二叉树结束的条件是后序遍历的区间为空(postL > postR)。

4、注意控制最后一个空格不要输出,可以设置一个计数器,为N时不输出空格。

A1020. Tree Traversals的更多相关文章

  1. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

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

  2. A1020 Tree Traversals (25 分)

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

  3. PAT A1020 Tree Traversals(25)

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

  4. PAT甲级——A1020 Tree Traversals

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

  5. [PAT] A1020 Tree Traversals

    [题目] distinct 不同的 postorder 后序的 inorder 中序的 sequence 顺序:次序:系列 traversal 遍历 题目大意:给出二叉树的后序遍历和中序遍历,求层次遍 ...

  6. A1020. Tree Traversals(25)

    这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列. 这题的核心: 建树 BFS #include<bits/stdc++.h> using names ...

  7. PAT_A1020#Tree Traversals

    Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ...

  8. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  9. HDU 1710 二叉树的遍历 Binary Tree Traversals

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. apache工作模式总结及网站访问缓慢处理记录

    apache目前主要有两种模式:prefork模式和worker模式:1)prefork模式(默认模式)prefork是Unix平台上的默认(缺省)MPM,使用多个子进程,每个子进程只有一个线程.每个 ...

  2. wordcount程序中的应用与拓展

    设计思路: 关键是思路,首先知道 单词, 行,字符, 他们有什么特点: 1.单词,标准的是遇到空格后,单词数,自动加一. 2.行是以\n结束的, 也就是说, 遇到\n行数加一,当然也视你的操作系统而言 ...

  3. python实现线性规划

    python工具包scipy linprog 函数格式 scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bo ...

  4. 12.14 Daily Scrum

      Today's Task Tomorrow's Task 丁辛 实现和菜谱相关的餐厅列表. 实现和菜谱相关的餐厅列表.             邓亚梅             美化搜索框UI. 美 ...

  5. Linux内核及分析 第一周 计算机是如何工作的?

    C语言代码: int g(int x) { return x + 5; } int f(int x) { return g(x); } int main(void) { return f(5) + 1 ...

  6. Docker for windows 入门一(下载安装)

    预安装条件,可以查阅官方文档,本人是Win10 x64(必要条件)教育版+开启Hyper-V(Feature特性),具体可参考云栖社区的文章: https://yq.aliyun.com/articl ...

  7. mysql 备份数据库 mysqldump

    @echo off for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE ...

  8. Flatpak 1.1.0发布:可终止运行Flatpak实例

    读 Flatpak的Alex Larsson发布了流行的Linux应用程序沙盒和分发框架的新版本,该框架有望成为跨Linux操作系统的应用程序分发的未来. Flatpak 1.1.0现已作为开始推出F ...

  9. ref、out与params

    ref 把值传递转换为引用传递,侧重于将一个值带到函数中进行改变,再将改变后的值带出去,ref参数在函数外必须为ref参数赋值 ; AddSalary(ref salary); //如果不写ref,s ...

  10. BZOJ1798[Ahoi2009]维护序列——线段树

    题目描述     老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成.    有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2 ...