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. php svn仓库提交预处理

    需要做的事情 1.检查是否填写注释2.php文件是否有语法错误 pre-commit脚本 hook脚本名称:hooks/pre-commit REPOS="$1" TXN=&quo ...

  2. OC第六课

    主要内容:Block(Block语法.Block使用.Block实现数组排序) 一.Block语法 Block:块语法.本质上是匿名函数(没有函数名称的函数),和函数指针非常相似 函数回想: 实现了某 ...

  3. $().attr()的使用方法 &amp;&amp; $().html()与$().text()的差别

    <1>$().attr()的使用方法 </pre><pre class="html" name="code"><htm ...

  4. NTC 家电中的应用1

    NTC应用非常广,特别是家电产品中,基本上凡是涉及到温度控制的都会选择它.本人应用的的产品有打奶器.电烤箱,油炸锅,咖啡机,直发器,暖风机和电壁炉,尽管应用广.但都是千篇一律.当中最基本的要点是应用中 ...

  5. ibatis 一对多查询

    <typeAlias alias="businessScopeItem" type="com.sdfrdj.vo.BusinessScopeItem"/& ...

  6. hdoj--5100--Chessboard(数学推理)

     Chessboard Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  7. B5248 [2018多省省队联测]一双木棋 状压dp

    这个题当时划水,得了二十分,现在来整一整. 这个题用状压来压缩边界线,然后通过记忆化搜索进行dp.我们可以观察到,其实每次转移,就是把一个1向左移一位.最后的状态设为0. 这其中还要有一个变量来记录谁 ...

  8. 洛谷 P3128 [ USACO15DEC ] 最大流Max Flow —— 树上差分

    题目:https://www.luogu.org/problemnew/show/P3128 倍增求 lca 也写错了活该第一次惨WA. 代码如下: #include<iostream> ...

  9. bzoj1179 [Apio2009]Atm——缩环最长路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 tarjan 缩环,然后求到有酒吧的点的最长路即可: 但一开始想缩环后用拓扑序求答案, ...

  10. Java压缩技术(三) ZIP解压缩——Java原生实现

    原文:http://snowolf.iteye.com/blog/642492 JavaEye的朋友跟我说:“你一口气把ZIP压缩和解压缩都写到一个帖子里,我看起来很累,不如分开好阅读”.ok,面向读 ...