PAT_A1127#ZigZagging on a Tree
Posted blue-lin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT_A1127#ZigZagging on a Tree相关的知识,希望对你有一定的参考价值。
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.
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:
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<cstdio> 12 #include<stack> 13 #include<queue> 14 using namespace std; 15 const int M=35; 16 int in[M],post[M]; 17 struct node 18 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<node*> q; 42 stack<node*> 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 else 61 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_JUDGE 82 #else 83 freopen("Test.txt", "r", stdin); 84 #endif 85 86 int n; 87 scanf("%d", &n); 88 for(int i=0; i<n; i++) 89 scanf("%d", &in[i]); 90 for(int i=0; i<n; i++) 91 scanf("%d", &post[i]); 92 node *root = Create(0,n-1,0,n-1); 93 Travel(root); 94 95 return 0; 96
以上是关于PAT_A1127#ZigZagging on a Tree的主要内容,如果未能解决你的问题,请参考以下文章
1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30分)
1127 ZigZagging on a Tree (30分)
PAT 1127 ZigZagging on a Tree (30)