1127 ZigZagging on a Tree (30 分)
Posted zllwxm123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1127 ZigZagging on a Tree (30 分)相关的知识,希望对你有一定的参考价值。
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
由后序和中序确定二叉树。
然后就随便就能输出来了。
1 #include <bits/stdc++.h> 2 using namespace std; 3 int n; 4 struct Node 5 6 int val; 7 Node *left, *right; 8 *root; 9 int inorder[50], post[50]; 10 struct mat 11 int val, ord, pri; 12 ; 13 bool cmp(mat &a, mat &b) 14 return a.ord < b.ord; 15 16 vector<mat> vt; 17 vector<int> v; 18 Node *create(int x, int ll, int rr) 19 Node *node = (Node*)malloc(sizeof(Node)); 20 node->val = post[x]; 21 node->left = NULL, node->right = NULL; 22 int mid = 0; 23 for(int i = ll; i <= rr; i++) 24 if(inorder[i] == post[x]) 25 mid = i; 26 break; 27 28 29 if(ll < mid) 30 node->left = create(x-rr+mid-1, ll, mid-1); 31 32 if(rr > mid) 33 node->right = create(x-1, mid+1, rr); 34 35 return node; 36 37 38 void output(Node *root, int x, int y) 39 if(root != NULL) 40 output(root->left, x<<1, y+1); 41 vt.push_back(root->val, x, y); 42 output(root->right, (x<<1)+1, y+1); 43 44 45 46 int main() 47 cin >> n; 48 for(int i = 0; i < n; i++) 49 cin >> inorder[i]; 50 51 for(int i = 0; i < n; i++) 52 cin >> post[i]; 53 54 root = create(n-1, 0, n-1); 55 output(root, 1, 1); 56 sort(vt.begin(), vt.end(), cmp); 57 for(int i = 0; i < vt.size(); i++) 58 if(vt[i].pri%2 == 0) 59 if(vt[i].pri != vt[i-1].pri) 60 int j = i-1; 61 while(vt[j].pri == vt[i-1].pri) 62 printf("%d ", vt[j].val); 63 j--; 64 65 66 printf("%d%c",vt[i].val, i != n-1?‘ ‘:‘\n‘); 67 68 69 int j = vt.size()-1; 70 if(vt[j].pri%2 == 1) 71 while(vt[j].pri == vt[n-1].pri) 72 v.push_back(vt[j].val); 73 j--; 74 75 for(int i = 0; i < v.size(); i++) 76 printf("%d%c", v[i], i == v.size()-1?‘\n‘:‘ ‘); 77 78 79 return 0; 80
以上是关于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)
PAT_A1127#ZigZagging on a Tree