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

 #include<cstdio>
#include<queue>
using namespace std;
int post[],in[];
struct node{
int data;
node *lchild;
node *rchild;
};
node *create(int postL,int postR,int inL,int inR){
if(postL>postR){
return NULL;
}
node *root=new node;
root->data=post[postR];
int k;
for(k=inL;k<=inR;k++){
if(post[postR]==in[k]){
break;
}
}
int numleft=k-inL;
root->lchild=create(postL,postL+numleft-,inL,k-);//这里经常出错
root->rchild=create(postL+numleft,postR-,k+,inR);
return root;
}
void LayerOrder(node*root,int k){
if(root==NULL){
return;
}
queue<node*> q;
q.push(root);
while(!q.empty()){
node *now=q.front();
q.pop();
if(k!=){
printf("%d ",now->data);
k--;
}else{
printf("%d",now->data);
}
if(now->lchild!=NULL){
q.push(now->lchild);
}
if(now->rchild!=NULL){
q.push(now->rchild);
}
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&post[i]);
}
for(int i=;i<n;i++){
scanf("%d",&in[i]);
}
node *p=create(,n-,,n-);
LayerOrder(p,n);
return ;
}

Mist Note: 解决本题,本小编的思路是,先利用中序和后序序列重建二叉树,然后再层序遍历这棵树。

很多算法还是基础的,主要是在递归重建那里经常出问题。左子树和右子树的递归。

root->lchild=create(postL,postL+numleft-1,inL,k-);

root->rchild=create(postL+numleft,postR-,k+,inR);

红色部分经常出错,切记,我们在写代码的时候,经常是利用数组,而数组是以0开始的,而numleft是个数,你加之后,肯定需要-1,才符合定义,不要混淆。

本题还需要注意输出格式,最后层序遍历的最后一个节点,是不需要空格的。

 

A1020 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 and i ...

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

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

  7. A1020. Tree Traversals(25)

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

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

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

  9. 【PAT】1043 Is It a Binary Search Tree(25 分)

    1043 Is It a Binary Search Tree(25 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  10. 1110 Complete Binary Tree (25 分)

    1110 Complete Binary Tree (25 分) Given a tree, you are supposed to tell if it is a complete binary t ...

随机推荐

  1. Codeforces Round #364 (Div. 2) B

    Description Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is e ...

  2. 自定义ClassLoader加载class文件

    package com.yd.wmsc.util; public class Test { public void say(){ System.out.println("Say Hello& ...

  3. C++学习 - 虚表,虚函数,虚函数表指针学习笔记

    http://blog.csdn.net/alps1992/article/details/45052403 虚函数 虚函数就是用virtual来修饰的函数.虚函数是实现C++多态的基础. 虚表 每个 ...

  4. (转)SELinux是什么意思,如何关闭?Linux下的防火墙用什么命令打开?

    SELinux是什么意思,如何关闭?Linux下的防火墙用什么命令打开? 原文:http://blog.csdn.net/hhcccchh/article/details/12995539 SELin ...

  5. iis mvc html

    iis mvc项目显示view文件夹下的html <system.webServer><handlers> <add name="JavaScriptHandl ...

  6. Likely root cause: java.lang.IllegalStateException: jar hell!

    jar hell 解决方案: 问题: Likely root cause: java.lang.IllegalStateException: jar hell! 解决: 当一一个类或者一一个资源文件存 ...

  7. 《Head First 设计模式》之策略模式——鸭子行为

    策略模式(Strategy Pattern) ——定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. (每个功能的多种实现成为一个算法族,这些算法族被分别封装 ...

  8. python中函数的定义与调用

    1.为什么要用函数? (1)代码重复太多(2)可读性差 使用函数的好处: (1)代码重用 (2)保持一致性,易维护 (2)可扩展性 2.初始函数定义与调用     函数的定义 def test(x): ...

  9. webpake-node-sass 报错

    问题描述: npm run dev 就报错,在安装node-sass错误 解决方法 : 找到node_modules下的node-sass文件,进入,如果没有vendor文件夹,就创建一个空文件夹,命 ...

  10. IOS微信6.7.4输入框失去焦点,软键盘关闭后,被撑起的页面无法回退到原来正常的位置

    近期在开发微信H5页面时碰到这个问题,如图,软键盘弹起后,若原输入框被遮挡,页面整体将会上移,然而当输入框失焦,软键盘收起后,页面未恢复,这也是ios的微信版本更新6.7.4之后才遇到的bug. 目前 ...