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. 闭包----你所不知道的JavaScript系列(4)

    一.闭包是什么? · 闭包就是可以使得函数外部的对象能够获取函数内部的信息. · 闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. · 闭包就 ...

  2. SpringBoot日记——Spring的安全配置-登录认证与授权

    安全是每个项目开发中都需要考虑的,比如权限控制,安全认证,防止漏洞攻击等. 比较常见的安全框架有:Apache的shiro.Spring Security等等,相信用shiro的用户群体更多,而sec ...

  3. CentOS7中安装redis5.0

    1. 环境介绍 CentOS7 (未安装Development Tools) 2. 下载Redis5.0-rc3 wget -O redis-5.0-rc3.tar.gz https://github ...

  4. iOS实时查看App运行日志

    前言: 本文讨论如何实时查看输出在console控制台的日志. 一.Xcode 通过Window->Devices打开devices界面,选择我们的手机,也能看到手机中运行的进程输出的日志.如图 ...

  5. C_数据结构_递归自己调用自己

    # include <stdio.h> void f(int n) { ) printf("哈哈\n"); else f(n-i); } int main(void) ...

  6. 20135337——linux实践三:ELF文件格式分析(32位系统)

    ELF文件格式分析 可重定位文件 十六进制形式显示内容 显示各个段.符号表相关信息 查看各个段信息 elf文件头信息 段表 符号表信息 查看堆栈 具体分析 1.ELF文件头信息(小字节优先,均十六进制 ...

  7. "留拍"-注册/登录详解

    1. 注册 打开 “留拍” 软件,进入 主页面 ,然后按 注册 按钮: 在注册页面什么内容 都没有写 上去的情况下,按 完成 按钮: 首先把URL封装起来: public class URL { pu ...

  8. 第三个sprint冲刺第二阶段

    内测版:

  9. 对TCP重传的进一步认识

    http://blog.sina.com.cn/s/blog_4d276ac901011ee7.html ——TCM项目所得 一.看图说话 1.基于套接字的TCP服务器/客户端程序流程 2.TCP三次 ...

  10. node基础 npm、module、exports、require

    module 模块.包:可以认为是一个代码包,package,提供特定的功能(暴露给外界接口,让外界调用) exports 输出.导出:导出模块中的各种类型的变量,以及各种方法,导出之后,才可以被外界 ...