PAT甲级——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
题目要求输出蛇形的层序序列。
先根据中序序列和后序序列建树,这一步是一个简单的分割;接下来输出蛇形层序序列,可用双端队列deque进行,观察到偶数层正向输出,奇数层反向输出,设置一个标志来确定层数奇偶,分别不同处理即可。
AC代码:
#include<iostream>
#include<string>
#include<map>
#include<iterator>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include <utility>
#include <fstream>
#include <ctime>
#define INF 0x7f7f7f7f
typedef long long ll;
using namespace std;
struct node {
int dep;
int data;
node* left, * right;
};
class tree {
private:
node* root;
public:
void create(int in[],int post[],int left,int right,int dep);
void create(node*& sub, int in[], int post[], int left, int right,int dep);
void level();
void level(node* sub);
};
void tree::create(int in[], int post[], int left, int right,int dep) {
create(root, in, post, left, right,dep+1);
}
void tree::create(node*& sub, int in[], int post[], int left, int right,int dep) {
if (left == right) {
sub = new node;
sub->data = post[right];
sub->dep = dep;
create(sub->left, in, post, left, right - 1,dep+1);
create(sub->right, in, post, left, right - 1,dep+1);
}
else if (left < right) {
int mid = right;
sub = new node;
sub->data = post[right];
sub->dep = dep;
for (int i = left; i <= right; i++) {
if (in[i] == post[right]) {
mid = i;
for (int j = mid; j < right; j++)
in[j] = in[j + 1];
break;
}
}
create(sub->left, in, post, left, mid - 1,dep+1);
create(sub->right, in, post, mid, right - 1,dep+1);
}
else {
sub = NULL;
}
}
void tree::level() {
level(root);
}
void tree::level(node* sub) {
int flag = 1;
deque<node*> q;
q.push_back(root);
int state = 0;
while (!q.empty()) {
if (!state) {
node* tmp = q.back();
if (!(tmp->dep % 2)) {
state = 1;
continue;
}
if (tmp->right)
q.push_front(tmp->right);
if (tmp->left)
q.push_front(tmp->left);
if (flag) {
cout << tmp->data;
flag = 0;
}
else
cout << " " << tmp->data;
q.pop_back();
}
else {
node* tmp = q.front();
if (tmp->dep % 2) {
state = 0;
continue;
}
if (tmp->left)
q.push_back(tmp->left);
if (tmp->right)
q.push_back(tmp->right);
if (flag) {
cout << tmp->data;
flag = 0;
}
else
cout << " " << tmp->data;
q.pop_front();
}
}
}
int main() {
int n;
int in[35], post[35];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> in[i];
}
for (int i = 0; i < n; i++) {
cin >> post[i];
}
tree t;
t.create(in, post, 0, n - 1, 0);
t.level();
return 0;
}