Source:

PAT A1127 ZigZagging on a Tree (30 分)

Description:

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

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 inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the zigzagging sequence of the tree in a line. 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:

8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1

Sample Output:

1 11 5 8 17 12 20 15

Keys:

Attention:

  • 开始方向弄反了-,-

Code:

 /*
Data: 2019-05-31 21:17:07
Problem: PAT_A1127#ZigZagging on a Tree
AC: 33:50 题目大意:
假设树的键值为不同的正整数;
给出二叉树的中序和后序遍历,输出二叉树的“之字形”层次遍历; 基本思路:
建树,层次遍历,依次用队列和堆存储结点值,输出即可
*/ #include<cstdio>
#include<stack>
#include<queue>
using namespace std;
const int M=;
int in[M],post[M];
struct node
{
int data,layer;
node *lchild,*rchild;
}; node *Create(int postL, int postR, int inL, int inR)
{
if(postL > postR)
return NULL;
node *root = new node;
root->data = post[postR];
int k;
for(k=inL; k<=inR; k++)
if(in[k]==root->data)
break;
int numLeft = k-inL;
root->lchild = Create(postL, postL+numLeft-, inL,k-);
root->rchild = Create(postL+numLeft, postR-, k+,inR);
return root;
} void Travel(node *root)
{
queue<node*> q;
stack<node*> s;
root->layer=;
q.push(root);
while(!q.empty())
{
root = q.front();q.pop();
if(root->layer%==)
{
while(!s.empty())
{
printf(" %d", s.top()->data);
s.pop();
}
printf(" %d", root->data);
}
else{
if(root->layer==)
printf("%d", root->data);
else
s.push(root);
}
if(root->lchild){
root->lchild->layer=root->layer+;
q.push(root->lchild);
}
if(root->rchild){
root->rchild->layer=root->layer+;
q.push(root->rchild);
}
}
while(!s.empty())
{
printf(" %d", s.top()->data);
s.pop();
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &in[i]);
for(int i=; i<n; i++)
scanf("%d", &post[i]);
node *root = Create(,n-,,n-);
Travel(root); return ;
}

PAT_A1127#ZigZagging on a Tree的更多相关文章

  1. PAT1127:ZigZagging on a Tree

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. 1127 ZigZagging on a Tree (30 分)

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  3. PAT甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

  5. PAT 1127 ZigZagging on a Tree[难]

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  6. pat 甲级 1127. ZigZagging on a Tree (30)

    1127. ZigZagging on a Tree (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  7. A1127. ZigZagging on a Tree

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  8. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  9. PAT 甲级 1127 ZigZagging on a Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805349394006016 Suppose that all the k ...

随机推荐

  1. DTrace Probes In MySQL 自定义探针

    Inserting user-defined DTrace probes into MySQL source code is very useful to help user identify the ...

  2. 一个oracle bug

    最近发现一个RAC db的listener log增长特别快,于是去查看了一下. 先是查看了一下log的内容,发现都是 service_update这种内容,刷新的特别快. service_updat ...

  3. 使用golang来设计我们的Ubuntu Scope

    我们知道golang越来越被非常多的开发人员来开发应用.go语言也能够用于开发Ubuntu Scope. 在今天的教程中.我们将具体介绍怎样使用go语言来开发我们的Scope.这对于非常多的不太熟悉C ...

  4. Qt graphic item日记

    今天在用用graphic view 加入graphic item的时候要引入一个context menu,自然就要对context menu上的action进行slot处理.可是graphic ite ...

  5. Extjs grid 某列点击弹窗

    { header : "单号", tooltip : '单号', dataIndex : 'transportCode', width : 130, sortable : true ...

  6. luogu1197 [JSOI2008]星球大战

    题目大意 有一个无向图,每次删除一个节点,求删除后图中连通块的个数.(如果两个星球可以通过现存的以太通道直接或间接地连通,则这两个星球在同一个连通块中) 题解 连通块?用并查集可以找到一个连通块,但是 ...

  7. oc37--类工厂方法

    // // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int ag ...

  8. nyoj--496--巡回赛(拓扑排序)

    巡回赛 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 世界拳击协会(WBA)是历史最悠久的世界性拳击组织,孕育了众多的世界冠军,尤其是重量级,几乎造就了大家耳熟能详的所 ...

  9. [CTSC 2008] 祭祀

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1143 [算法] 答案为最小路径可重复点覆盖所包含的路径数,将原图G进行弗洛伊德传递闭 ...

  10. 昂贵的聘礼(Dijkstra)

    http://poj.org/problem?id=1062 每个物品看成一个节点,酋长的允诺也看作一个物品, 如果一个物品加上金币可以交换另一个物品,则这两个节点之间有边,权值为金币数,求第一个节点 ...