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 ...
随机推荐
- Android进阶笔记10:Android 万能适配器
1. Android 万能适配器 项目中Listview GridView几乎是必用的组件,Android也提供一套机制,为这些控件绑定数据,那就是Adapter.用起来虽然还不错,但每次都 ...
- ReactNative 大图手势浏览技术分析
支持通用的手势缩放,手势跟随,多图翻页 手势系统 通过 PanResponder.create 创建手势响应者,分别在 onPanResponderMove 与 onPanResponderRelea ...
- cocos2d-x——在一个cpp中展示多个场景
//20秒后自动运行下一个场景 runAction( CCSequence::create(CCDelayTime::create(20.0f), CCCallFunc::create(this, c ...
- 求可能组合VB源码代写
输入1-20的整数n,把从1到n的n个整数摆成一个环,使得该环上任意相邻的两个数之和为素数.求出所有可能组合. 专业程序代写c++程序代写
- mac 如何进入/usr/sbin目录
1.进入terminal, 输入 ls /usr/sbin 2.在finder>前往文件夹,输入路径/usr/sbin
- ACM进阶
ACM队不是为了一场比赛而存在的,为的是队员的整体提高. 大学期间,ACM队队员必须要学好的课程有: l C/C++两种语言 l 高等数学 l 线性代数 l 数据结构 l 离散数学 l 数据库原理 l ...
- 网易新闻RSS阅读器
首先需要分析网易RSS订阅中心的网页布局情况. 网易RSS订阅中心:http://www.163.com/rss/ 你会发现RSS文件由一个<channel>元素及其子元素组成,除了频道本 ...
- 【Android学习】调用系统短信、电话
今天全心投入Android学习已经有一段时间了,从起初的啥也不懂,到现在半知半解状态,随笔记录些简单且常用的系统功能代码. 调用Android系统短信,其实调用短信非常简单,一个方法就可以搞定.我们可 ...
- selenium Grid(一)
selenium grid Quick Start selenium-grid是用于设计帮助我们进行分布式测试的工具,其整个结构是由一个hub节点和若干个代理节点组成.hub用来管理各个代理节点的注册 ...
- 在慕课学习Bootstrap
---恢复内容开始--- 可以用class=“h1”等给元素加h1样式 <h>------<small>----</small></h> < ...