A1127. ZigZagging on a Tree

时间:2021-06-23 04:33:18

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.

A1127. ZigZagging on a Tree

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
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef struct NODE{
struct NODE* lchild, *rchild;
int data;
int lev;
}node;
int post[], in[], N;
node* create(int inL, int inR, int postL, int postR, int level){
if(inL > inR){
return NULL;
}
node* root = new node;
root->data = post[postR];
root->lev = level;
int mid;
for(mid = inL; mid <= inR; mid++){
if(in[mid] == root->data)
break;
}
int len = mid - inL;
root->lchild = create(inL, mid - , postL, postL + len - , level + );
root->rchild = create(mid + , inR, postL + len, postR - , level + );
return root;
}
void levelOrder(node* root){
queue<node*> Q;
Q.push(root);
int nowLev = root->lev;
int reverTag = ;
int cnt = ;
vector<node*> vec;
while(Q.empty() == false){
node* temp = Q.front();
Q.pop();
if(temp->lev == nowLev){
vec.push_back(temp);
}else{
nowLev = temp->lev;
if(reverTag == ){
reverTag = ;
for(int i = vec.size() - ; i>= ; i--){
cnt++;
printf("%d ", vec[i]->data);
}
}else{
reverTag = ;
for(int i = ; i < vec.size(); i++){
cnt++;
printf("%d ", vec[i]->data);
}
}
vec.clear();
vec.push_back(temp);
}
if(temp->lchild != NULL)
Q.push(temp->lchild);
if(temp->rchild != NULL)
Q.push(temp->rchild);
}
if(reverTag == ){
for(int i = ; i < vec.size(); i++){
cnt++;
if(cnt == N)
printf("%d", vec[i]->data);
else printf("%d ", vec[i]->data);
}
}else{
for(int i = vec.size() - ; i >= ; i--){
cnt++;
if(cnt == N)
printf("%d", vec[i]->data);
else printf("%d ", vec[i]->data);
}
}
}
int main(){
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, );
levelOrder(root);
cin >> N;
return ;
}

总结:

1、题意:中序后序建树,再用层序输出,输出时一行逆序一行正序。

2、可以在建树时顺便将层次信息也加入节点。在层序遍历时使用一个vector,在当前层数相同时,仅仅把本该访问的节点存入vector,当层数发生改变时,输出vector内所有元素(设置一个计数器,如果上次是正序输出,则本次逆序输出); 或者正常层序遍历,再多用一个栈+一个队列,当需要正序输出该层时,节点入队列,需要逆序则节点入栈,层数改变时输出栈中或队列中全部节点,并清空。