1020 Tree Traversals (25)(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

C++代码如下:

 #include<iostream>
#include<queue>
using namespace std; struct node {
int data;
node *lchild, *rchild;
}; int post[], in[];
int n; node* create(int posL,int posR,int inL,int inR) {
if (posL>posR) return NULL;
node *root = new node;
root->data = post[posR];
int k;
for (k = inL; k <=inR; k++) {
if (in[k] == post[posR]) break;
}
int numL = k-inL;
root->lchild = create(posL, posL + numL-, inL, k - );
root->rchild = create(posL + numL, posR - , k + , inR); return root;
} int num = ; void level(node*r) {
if (r == NULL) return;
queue<node*>q;
q.push(r);
while (!q.empty()) {
node *top = q.front();
q.pop();
num++;
cout << top->data;
if (num < n) cout << ' ';
if (top->lchild != NULL) q.push(top->lchild);
if (top->rchild != NULL) q.push(top->rchild);
}
} int main() {
cin >> n;
int t;
for (int i = ; i < n; i++) {
cin >> t;
post[i] = t;
}
for (int i = ; i < n; i++) {
cin >> t;
in[i] = t;
}
node*root = create(, n - , , n - );
level(root);
return ;
}

【PAT】1020 Tree Traversals (25)(25 分)的更多相关文章

  1. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

  2. PAT 1020 Tree Traversals[二叉树遍历]

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

  3. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  4. 1020 Tree Traversals (25 分)(二叉树的遍历)

    给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...

  5. PAT A1020 Tree Traversals(25)

    题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

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

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

  7. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

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

  8. PAT Advanced 1020 Tree Traversals (25 分)

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

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

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

随机推荐

  1. Enum 枚举值 (一) 获取描述信息

    封装了方法: public static class EnumOperate { public class BaseDescriptionAttribute : DescriptionAttribut ...

  2. noip2017颓废记

    作为一个从初中就开始学信息的蒟蒻,自然要去提高组了~~~ 比赛前day1 跟平常一样在机房颓废着,上午在洛谷看到了站长大人的忠告后,看了看模板题,发现没几个会打的(正常). 下午想一想发现自己的dp垃 ...

  3. 简单prufer应用

    [bzoj1005] Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? ...

  4. 解题:九省联考2018 秘密袭击CoaT

    题面 按照*Miracle*的话来说,网上又多了一篇n^3暴力的题解 可能是因为很多猫题虽然很好,但是写正解性价比比较低? 直接做不可做,转化为统计贡献:$O(n)$枚举每个权值,直接统计第k大大于等 ...

  5. Eclipse启动项目正常,放到tomcat下单独启动就报错的 一例

    一个老的ssh的项目,进行二次开发(增加一些新功能)后, 首先用Eclipse中集成的Tomcat启动没有任何问题,但是把启动后的webapps下得目录放到 windows的普通tomcat下单独启动 ...

  6. Go_22: Golang 命令行 test 应用

    1. 测试某一个包下的所有测试用例 cd /myGoProject/src/go-test/utils go test -v "-v" 参数 go test -v ... 表示无论 ...

  7. Windows平台上谷歌浏览器损害电池

    From:http://www.cnblogs.com/killerlegend/p/3909208.html Author:KillerLegend Date:2014.8.13 事情是这样的,我的 ...

  8. bzoj千题计划212:bzoj1864: [Zjoi2006]三色二叉树

    http://www.lydsy.com/JudgeOnline/problem.php?id=1864 #include<cstdio> #include<cstring> ...

  9. Spring RedisTemplate操作-通道操作(10)

    @Autowired @Resource(name = "redisTemplate") private RedisTemplate<String, String> r ...

  10. 20155325 2016-2017-2 《Java程序设计》第8周学习总结

    教材学习内容总结 NIO用于高级输入/输出处理 名称 衔接数据源与目的地 IO InputStream,OutputStream NIO Channel 类 方法 方法的作用 ReadableByte ...