【PAT】1020. Tree Traversals (25)
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
分析:考察树的建立和遍历。
课参考《编程之美》3.9
#include<iostream>
#include<map>
#include<vector>
#include<queue>
using namespace std; struct Node{
Node *left;
Node *right;
int value;
Node():left(NULL),right(NULL){}
}; void Rebuild(int * PostOrder, int * InOrder, int len, Node* &root){
//判断何时结束递归
if(PostOrder == NULL || InOrder == NULL)
{
root = NULL;
return ;
}
if(root == NULL) root = new Node;
root->value = *(PostOrder + len - 1);
root->left = NULL;
root->right = NULL;
if(len == 1)
return; int count = 0;
int *temp = InOrder;
while(*temp != *(PostOrder + len -1))
{
count ++;
temp++;
if(count > len) break;
}
int left = temp - InOrder ;
int right = len - left - 1;
if(left > 0)
Rebuild(PostOrder, InOrder, left, root->left);
if(right > 0)
Rebuild(PostOrder + left, InOrder+left+1, right, root->right);
} int main()
{
int n,i,t;
while(cin>>n)
{
int *PostOrder = new int[n];
int *InOrder = new int[n];
for(i=0; i<n; i++)
cin>>PostOrder[i];
for(i=0; i<n; i++)
cin>>InOrder[i]; Node *root = new Node;
int post_start,in_start;
post_start = 0;
in_start = 0;
Rebuild(PostOrder, InOrder, n, root);
queue<Node *> q;
q.push(root);
int flag = 1; while(!q.empty()){
if(q.front()->left != NULL)
q.push(q.front()->left);
if(q.front()->right != NULL)
q.push(q.front()->right);
if(flag != n)
cout<<q.front()->value<<" ";
else
cout<<q.front()->value;
flag ++;
q.pop();
} cout<<endl;
}
return 0;
}
【PAT】1020. Tree Traversals (25)的更多相关文章
- 【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 (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 ...
- 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 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT (Advanced Level) 1020. Tree Traversals (25)
递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...
- 1020. Tree Traversals (25)
the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...
随机推荐
- jquerymobile使用技巧
1)ajax开关(默认jquery以ajax方式加载页面) $.mobile.ajaxEnabled = false; 2)不编译指定标签 $.mobile.page.prototype.option ...
- Http进行网络通信
http使用get的方式进行网络通信: package com.testGet; import java.io.BufferedReader; import java.io.IOException; ...
- iso中自动伸缩属性
一.自动伸缩属性 UIViewAutoresizingNone 不伸缩 UIViewAutoresizingFlexibleLeftMargin 跟父控件左边的距离 ...
- 2015-10-14 晴 tcp/ip
今天看完ping, traceroute, ip选路,动态选路协议,dup, igmp, tftp, bootp,tcp
- linux echo命令的-n、-e两个参数
echo -n 不换行输出 $echo -n "123" $echo "456" 最终输出 123456 而不是 123 456 echo -e 处理特殊字符 ...
- mysql 添加索引后 在查询的时候是mysql就自动从索引里面查询了。还是查询的时候有单 独的参数查询索引?
MYSQL在创建索引后对索引的使用方式分为两种:1 由数据库的查询优化器自动判断是否使用索引:2 用户可在写SQL语句时强制使用索引 下面就两种索引使用方式进行说明第一种,自动使用索引.数据库在收到查 ...
- Arduino 使用舵机库时 其它引脚输出怪异 解决方案
使用Servo.h时,不管你在初始化时用的是9还是10脚,都不要把这两个脚作为舵机以外的用途! 例: servo.attach(9); digitalWrite(10,1);//错,不能把第10脚用作 ...
- 嵌入式 Linux下编译并使用curl静态库
#x86 ./configure --disable-shared --enable-static --disable-ftp --disable-ipv6 --disable-rtsp --disa ...
- HDU 5127 Dogs' Candies
Dogs' Candies Time Limit: 30000/30000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) T ...
- 【初识——最大流】 hdu 1532 Drainage Ditches(最大流) USACO 93
最大流首次体验感受—— 什么是最大流呢? 从一个出发点(源点),走到一个目标点(汇点),途中可以经过若干条路,每条路有一个权值,表示这条路可以通过的最大流量. 最大流就是从源点到汇点,可以通过的最大流 ...