博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT_A1127#ZigZagging on a Tree
阅读量:6543 次
发布时间:2019-06-24

本文共 3300 字,大约阅读时间需要 11 分钟。

Source:

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.

zigzag.jpg

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:

812 11 20 17 1 15 8 512 20 17 11 15 8 5 1

Sample Output:

1 11 5 8 17 12 20 15

Keys:

  • 二叉树的建立
  • 二叉树的遍历

Attention:

  • 开始方向弄反了-,-

Code:

1 /* 2 Data: 2019-05-31 21:17:07 3 Problem: PAT_A1127#ZigZagging on a Tree 4 AC: 33:50 5  6 题目大意: 7 假设树的键值为不同的正整数; 8 给出二叉树的中序和后序遍历,输出二叉树的“之字形”层次遍历; 9 */10 11 #include
12 #include
13 #include
14 using namespace std;15 const int M=35;16 int in[M],post[M];17 struct node18 {19 int data,layer;20 node *lchild,*rchild;21 };22 23 node *Create(int postL, int postR, int inL, int inR)24 {25 if(postL > postR)26 return NULL;27 node *root = new node;28 root->data = post[postR];29 int k;30 for(k=inL; k<=inR; k++)31 if(in[k]==root->data)32 break;33 int numLeft = k-inL;34 root->lchild = Create(postL, postL+numLeft-1, inL,k-1);35 root->rchild = Create(postL+numLeft, postR-1, k+1,inR);36 return root;37 }38 39 void Travel(node *root)40 {41 queue
q;42 stack
s;43 root->layer=1;44 q.push(root);45 while(!q.empty())46 {47 root = q.front();q.pop();48 if(root->layer%2==0)49 {50 while(!s.empty())51 {52 printf(" %d", s.top()->data);53 s.pop();54 }55 printf(" %d", root->data);56 }57 else{58 if(root->layer==1)59 printf("%d", root->data);60 else61 s.push(root);62 }63 if(root->lchild){64 root->lchild->layer=root->layer+1;65 q.push(root->lchild);66 }67 if(root->rchild){68 root->rchild->layer=root->layer+1;69 q.push(root->rchild);70 }71 }72 while(!s.empty())73 {74 printf(" %d", s.top()->data);75 s.pop();76 }77 }78 79 int main()80 {81 #ifdef ONLINE_JUDGE82 #else83 freopen("Test.txt", "r", stdin);84 #endif85 86 int n;87 scanf("%d", &n);88 for(int i=0; i

 

转载于:https://www.cnblogs.com/blue-lin/p/10957755.html

你可能感兴趣的文章
vue 组件编码规范
查看>>
IEC61850与MMS的服务映射
查看>>
Java 泛型: 什么是PECS(Producer Extends, Consumer Super)
查看>>
软件包管理-打包解包压缩解压
查看>>
maven构建scala项目
查看>>
linux 高级编程看的书
查看>>
Memcached分布式缓存-windows上初步使用-网摘
查看>>
IIS无法启动的问题
查看>>
如何通过结构中的某个变量获取结构本身的指针?(container_of详解)
查看>>
Android 关于mnt/sdcard和sdcard的区别
查看>>
特征变换(7)总结
查看>>
网络工程师之路怎么走?
查看>>
go语言unix域套接字发送udp报文
查看>>
2.并发和并行
查看>>
OpenGL学习(二)用户与交互
查看>>
神奇的代码-常见错误代码注意点
查看>>
[直播一揽子]编码构思和套路
查看>>
[直播一揽子]x264参数的解释
查看>>
static的意义和功能
查看>>
iOS学习之Objective-C 2.0 运行时系统编程
查看>>