1020. Tree Traversals (序列建树)
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>
using namespace std;
struct LNode
{
LNode *lchild,*rchild;
int data;
};
int in[];
int pos[];
LNode* fun(int pos[],int f1,int r1,int in[],int f2,int r2)//生成树
{
if(f1>r1) return NULL;
LNode *p=(LNode*)malloc(sizeof(LNode));
p->lchild=NULL;
p->rchild=NULL;
p->data=pos[r1];
int i;
for(i=f2;i<=r2;i++)
if(in[i]==pos[r1]) break;
p->lchild=fun(pos,f1,f1+i-f2-,in,f2,i-);
p->rchild=fun(pos,f1+i-f2,r1-,in,i+,r2);
return p;
}
int main()
{
int n;
while(cin>>n)
{
int i;
for(i=;i<n;i++)
cin>>pos[i];
for(i=;i<n;i++)
cin>>in[i];
LNode *q=(LNode*)malloc(sizeof(LNode));
q=fun(pos,,n-,in,,n-);
LNode* que[]; //层次遍历
int front=;int rear=;
rear++;
que[rear]=q;
bool fir=true;
while(front!=rear)
{
front++;
if(fir)
{cout<<que[front]->data;fir=false;}
else cout<<" "<<que[front]->data;
if(que[front]->lchild!=NULL)
que[++rear]=que[front]->lchild;
if(que[front]->rchild!=NULL)
que[++rear]=que[front]->rchild;
}
cout<<endl;
}
return ;
}
1020. Tree Traversals (序列建树)的更多相关文章
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 1020 Tree Traversals——PAT甲级真题
1020 Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Give ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
随机推荐
- spring+hibernate+struts整合(2)
spring和struts2的整合 1:配置Web.xml文件 <filter> <filter-name>struts2</filter-name> <fi ...
- JavaScript中常用函数(入门级)(持续更新)
本文中枫竹梦介绍一些JavaScript中入门级的常用函数,对于已经过了入门的童鞋可选择略过,都是一些非常实用的函数.如果发现什么问题,欢迎讨论. 问题列表 Q1: 设计一个函数repeatIt(st ...
- java 网络编程-tcp/udp
--转自:http://blog.csdn.net/nyzhl/article/details/1705039 直接把代码写在这里,解释看这里吧:http://blog.csdn.net/nyzhl/ ...
- CentOS7.0 重置Root的密码
首先进入开启菜单,按下e键进入编辑现有的内核,如下图所示 然后滚动列表,找到ro,将它替换成rw,并加上init=/sysroot/bin/sh,最终变为如下图 然后按CTRL+X进入到单用户模式,在 ...
- [改善Java代码]在switch的default代码块中增加AssertionError错误
switch的后跟枚举类型,case后列出所有的枚举项,这是一个使用枚举的主流写法,那留着default语句似乎没有任何作用了,程序已经列举出了所有的可能选项,肯定不会执行到default语句,. 错 ...
- 关于关闭Eclipse的控制台自动跳出
参考文章: http://my.oschina.net/mn1127/blog/161093 Eclipse的控制台console有时候经常的跳出来,非常的烦人! 尤其是在调试期间跳出,以下是分享一下 ...
- 【模拟】UVa 12108 - Extraordinarily Tired Students
When a student is too tired, he can't help sleeping in class, even if his favorite teacher is right ...
- Delphi版本号对照(转)
Delphi版本号对照 VER10 :Turbo Pascal 1VER20 : Turbo Pascal 2VER30 : Turbo Pascal 3VER40 : Turbo Pasca ...
- 实现类似 QQ音乐网页版 的单页面总结
最近需要对创业团队的网站进行改版,而我负责前端设计和实现. 下面是一些总结与体会: 当设计完成之前,我就跟和我配合的Java 后台说用iframe实现,结果说麻烦不肯,到最后突然对我说还是用ifram ...
- ActiveMQ(5.10.0) - Message Redelivery and DLQ Handling
When messages expire on the ActiveMQ broker (they exceed their time-to-live, if set) or can’t be red ...