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. TOYS POJ 2318 计算几何 叉乘的应用

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15060   Accepted: 7270 Description Calc ...

  2. [转]十五天精通WCF——第三天 client如何知道server提供的功能清单

     通常我们去大保健的时候,都会找姑娘问一下这里能提供什么服务,什么价格,这时候可能姑娘会跟你口述一些服务或者提供一份服务清单,这样的话大 家就可以做到童嫂无欺,这样一份活生生的例子,在wcf中同样是一 ...

  3. 学习C#和SQL的书籍

    1. <21天学通C#> 周红安等编著  电子工业出版社 2.  http://blog.csdn.net/21aspnet/article/details/1682200 3. < ...

  4. codeforces Looksery Cup 2015 D. Haar Features

    The first algorithm for detecting a face on the image working in realtime was developed by Paul Viol ...

  5. WebView 用法总结

    1.AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会出 Web page not available 错误. ...

  6. 【NOI 2014】 动物园

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3670 [算法] KMP [代码] #include<bits/stdc++.h ...

  7. ubuntu 12.10 禁用触摸板

    1. 打开终端,输入 sudo rmmod psmouse 禁用触摸板,输入 sudo modprobe psmouse 恢复触摸板 2.syndaemon -i 10 -d >/dev/nul ...

  8. Bootstrap 只读输入框

    只读输入框 为输入框设置 readonly 属性可以禁止用户输入,并且输入框的样式也是禁用状态.   <input class="form-control" type=&qu ...

  9. Google Chrome调试常用快捷键

    Ctrl+Shift+I调出开发者工具 Ctrl+R刷新(界面显示不清楚,刷新后变清楚)

  10. PCB LDI文件 自动化输出(改造)实现思路

    由于工厂采用Liunxs系统输出LDI文件,由于我们数据库是用的Windows Server,编程语言是.net 无法与Liunxs系统进行有效对接, 所以造成才会造成LDI 资料输效率极低,人员工作 ...