题目:

1020. Tree Traversals (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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<iostream>
#include<queue>
using namespace std;

typedef struct _Node
{
int num;
struct _Node *lchild;
struct _Node *rchild;
}Node, *PNode;

int t1[1001],t2[1001];
int n;

int getPosition(int a){
int i;
for(i=1;i<=n;i++)
if(a == t2[i])
return i;
}

void createTree(PNode &node, int i, int j, int len){
if(len<=0){
return ;
}
node = new Node;
node->num = t1[i];
node->lchild = NULL;
node->rchild = NULL;
int m = getPosition(t1[i]);
createTree(node->lchild,i-len+m-j,j,m-j);//m-j:左子树len,len-1-(m-j):右子树len
createTree(node->rchild,i-1,m+1,len-1-m+j);
}

void PreTravelTree(PNode pn) //前序递归遍历
{
if(pn){
cout<<pn->num<<" "<<endl;
PreTravelTree(pn->lchild);
PreTravelTree(pn->rchild);
}

}

int main()
{
int i;
int r[100];
queue<PNode> q;
cin>>n;
for(i=1;i<=n;i++)
cin>>t1[i];
for(i=1;i<=n;i++)
cin>>t2[i];
PNode root = NULL,temp;
createTree(root,n,1,n);
q.push(root);
i=0;
while(!q.empty()){
temp = q.front();
q.pop();
r[i] = temp->num;
i++;
if(temp->lchild!=NULL)
q.push(temp->lchild);
if(temp->rchild!=NULL)
q.push(temp->rchild);
}
cout<<r[0];
for(i=1;i<n;i++)
cout<<" "<<r[i];
cout<<endl;
return 0;
}

PAT Advance 1020的更多相关文章

  1. PAT(B) 1020 月饼(Java)

    题目链接:1020 月饼 (25 point(s)) 分析 将月饼(库存量,总售价,单价)封装成MoonCake类 Scanner会超时,用BufferedReader类读取数据 读取的时候用字符串数 ...

  2. PAT乙级 1020. 月饼 (25)(只得到23分)

    1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...

  3. PAT Basic 1020

    1020 月饼 (25 分) 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意 ...

  4. PAT 乙级 1020 月饼 (25) C++版

    1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...

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

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

  6. PAT乙级1020

    1020 月饼 (25 分)   月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. ...

  7. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  8. pat甲级1020中序后序求层序

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

  9. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

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

随机推荐

  1. 我的Go语言学习之旅二:入门初体验 Hello World

    好吧,全部的程序猿们都已经习惯了.学习不论什么一门语言,我们都会以Hello World实例開始我们的学习,我也不例外.先来一个简单的样例 打开编辑器 (能够用记事本,我已经习惯 Notepad++了 ...

  2. CXF 调用方式——动态创建客户端(调用稳定版本号为2.7.18)

    今天用动态创建客户端的方式调用webservice,报了这样一个错: 2017-01-05 20:51:46,029 DEBUG main org.apache.cxf.common.logging. ...

  3. Drupal的钩子系统

    Drupal的很多功能都是可以定制的.以导航菜单为例,blog模块需要在菜单上添加一些功能,comment模块需要在菜单上添加一些功能,我们开发的自定义模块也需要在菜单上添加一些功能.Drupal开发 ...

  4. Could not create and/or set value back on to object .

    严重: Error building beanorg.springframework.beans.factory.UnsatisfiedDependencyException: Error creat ...

  5. 关于Xcode正确运行swift多线程

    想跳过废话直接看解决方案的可以点击这里直接跳转,我这人写博客喜欢瞎逼逼. 还有一些我看过的不错的多线程资料,在此给出链接,点击这里直接跳转查看. 近来为了做操作系统课程设计,不得不去学习了下多线程. ...

  6. AutoHotKey入门

    首先它要编译.ahk后缀的脚本才能执行.脚本里再写键盘触发监听之类的逻辑. 所以并非单单只是热键启动那么简单,可以组合出复杂的功能,甚至支持正则表达式 理论上扩展性比按键精灵差,易用性大大优于按键精灵 ...

  7. Android图片二级缓存

    点击下载源代码 想起刚開始写代码的时候,领导叫我写一个头像下载的方法,当时屁颠屁颠就写了一个图片下载的,每次都要去网络上请求,最后直接被pass掉了 当时的思路是这种 后来渐渐地就知道了有二级缓存这东 ...

  8. CvSplit

    /* possible split in the tree */ typedef struct CvSplit { CvTreeCascadeNode* parent; CvTreeCascadeNo ...

  9. IOS使用Charts

    近期项目中要做图表功能,为了降低开发量採用的是H5+ECharts来做.这里说一下IOS中怎样使用ECharts以及遇到的问题. 网络模块化单文件引入 <!DOCTYPE html> &l ...

  10. Java多线程之内置锁与显示锁

    Java中具有通过Synchronized实现的内置锁,和ReentrantLock实现的显示锁,这两种锁各有各的好处,算是互有补充,今天就来做一个总结. Synchronized 内置锁获得锁和释放 ...