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. NTP系统时间同步-操作记录

    在初始化一台linux服务器后,发现这台服务器的时间不对[root@dev ~]# date2016年 10月 11日 星期二 07:04:34 CST Linux时钟分为系统时钟 (System C ...

  2. 我的github地址

    链接:https://github.com/long0123/test.git   推送项目的github的大致步骤如下: 1.在本地创建一个项目仓库,可以放些基本的项目文件 2.cd至该目录下 3. ...

  3. 『编程题全队』Beta 阶段冲刺博客二

    1.提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID) (1) 昨天已完成的工作 孙志威: 1.添加了SubTask类,完成基本UI 2.为SubTask类添加了展开/收缩 ...

  4. (html4与html5分别实现)用一个表单向php界面提交不同的命令

    先看上面的表单实例,分别有3个submit,只不过需要根据需要进行选择添加.删除或是修改.现在有两个方案. 方案一: 如果实在HTML4中,可以用下面的代码实现, 即将所有的submit提交到acce ...

  5. Java代码安全

    https://www.owasp.org/index.php/Category:Java

  6. Enterprise Integration Patterns

    https://camel.apache.org/enterprise-integration-patterns.html 企业集成模式,各种模式算法,挺棒的. https://camel.apach ...

  7. mongo扩展错误

    make: *** [php_mongo.lo] Error 1 Ask Question 0   When I installed the Mongo PHP extension, the foll ...

  8. CentOS 离线安装Gitlab-ce

    1. 上gtilab的官网,找了一下安装说明.. 首先安装 依赖的包 sudo yum install -y curl policycoreutils-python openssh-server cr ...

  9. WebService概述

    一.WebService介绍 什么是WebService? 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序采用java编 ...

  10. python排序

    排序算法概览 归并排序的 空间复杂度为O(n) 插入排序 基本思想是每次讲一个待排序的记录,按其关键字大小插入到前面已拍好的子序列中,直到全部完成. 直接插入排序 讲元素L(i)插入到有序序列L[1, ...