【PAT】A1127 ZigZagging on a Tree (30point(s))


Author: CHEN, Yue
Organization: 浙江大学
Time Limit: 400 ms
Memory Limit: 64 MB
Code Size Limit: 16 KB

A1127 ZigZagging on a Tree (30point(s))

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:

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

Code

#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct NODE{
    int data,level;
    struct NODE *lchild,*rchild;
};
vector<int> postorder,inorder,res[50];
NODE *build(int postL,int postR,int inL,int inR){
    if(postL>postR)   return NULL;
    NODE* root=new NODE;
    root->data=postorder[postR];
    int i;
    for(i=inL;i<=inR;i++)   if(postorder[postR]==inorder[i])    break;
    int lcnt=i-inL;
    root->lchild=build(postL,postL+lcnt-1,inL,i-1);
    root->rchild=build(postL+lcnt,postR-1,i+1,inR);
    return root;
}
void zlevelorder(NODE *root){
    queue<NODE*> q;
    root->level=1;
    q.push(root);
    while(!q.empty()){
        NODE* temp=q.front();
        q.pop();
        res[temp->level].push_back(temp->data);
        if(temp->lchild!=NULL){
            temp->lchild->level=temp->level+1;
            q.push(temp->lchild);
        }
        if(temp->rchild!=NULL){
            temp->rchild->level=temp->level+1;
            q.push(temp->rchild);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    postorder.resize(n);
    inorder.resize(n);
    for(int i=0;i<n;i++)    scanf("%d",&inorder[i]);
    for(int i=0;i<n;i++)    scanf("%d",&postorder[i]);
    NODE *root=build(0,n-1,0,n-1);
    zlevelorder(root);
    printf("%d",res[1][0]);
    for(int i=2;i<50;i++){
        if(i%2==0)  for(int j=0;j<res[i].size();j++)    printf(" %d",res[i][j]);
        else    for(int j=res[i].size()-1;j>=0;j--) printf(" %d",res[i][j]);
    }
    return 0;
}

Analysis

-另类的层序输出。

-将数存放在一个二位数组res[][]中,第i层的内容存放在res[i]中,当i为奇数时逆向输出,当i为偶数时顺向输出。

发布了159 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ztmajor/article/details/103943557