题目:

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. Struts2的国际化入门

    Struts2的国际化入门 Struts2国际化是建立在Java国际化的基础上的,一样是通过提供不同国家/语言环境的消息资源,然后通过ResourceBundle加载指定Locale对应的资源文件,再 ...

  2. MQTT---HiveMQ源代码具体解释(七)Netty-SSL/NoSSL

    源博客地址:http://blog.csdn.net/pipinet123 MQTT交流群:221405150 实现功能 依据用户配置的不同的Listener(TcpListener.TlsTcpLi ...

  3. 【微信小程序】微信小程序wx.previewImage预览图片

    一.小知识 二.例子,配合轮播图使用效果更佳!(如图1) 1.wxml <scroll-view scroll-y="true"> <swiper catchta ...

  4. 【php】在php代码嵌入HTML代码(适用于公众号开发)

    核心:HTML的双引号["]一定要转义,不废话: $link = "<a href=\"http://www.baidu.com\">最新活动链接& ...

  5. ORA-01589: 要打开数据库则必须使用 RESETLOGS 或 NORESETLOGS 选项

    产生这个的原因可能是由于数据库突然停止,没有来得及将缓存区中的LOG归档,导致下次开启时不能匹配日志文件. 数据库中的三个日志文件挨个试,第二个就匹配上了

  6. 系统流畅/性能受限 谷歌Nexus4详细评测

    http://mobile.it168.com/a2012/1220/1437/000001437938_8.shtml

  7. atitit.基于  Commons CLI 的命令行原理与 开发

    atitit.基于  Commons CLI 的命令行原理与 开发 1. 命令行支持的格式有以下几种: 1 2. json化,map化的命令行参数内部表示 1 3. Ati cli 2 4. CLI库 ...

  8. mysql 一些常用指令

    登陆: mysql -u root -p //登陆,输入root密码 退出登陆 mysql>exit; mysql 为所有ip授权 mysql> GRANT ALL PRIVILEGES ...

  9. 一个简单题,引发的思索 + nyoj 1189

    题目描述:第一行:给你两个数m和n,m表示有m个数,然后下一行输入m个数,每个数只能选择一次,统计共有多少种情况使得所选数的和大于等于n: 解决本题我想到了两种方法,(题目自己想的,先不考虑超时),第 ...

  10. 写一个php小脚本辅助渗透测试

    因为一个注入要爬行一些数据,然后写的一个小脚本,能写脚本来辅助渗透,也算是里程碑.哈哈哈 <?php $num = 1; while ($num <= 39) { $web_url = & ...