zoukankan      html  css  js  c++  java
  • A1127. ZigZagging on a Tree

    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<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<queue>
     5 #include<vector>
     6 using namespace std;
     7 typedef struct NODE{
     8     struct NODE* lchild, *rchild;
     9     int data;
    10     int lev;
    11 }node;
    12 int post[31], in[31], N;
    13 node* create(int inL, int inR, int postL, int postR, int level){
    14     if(inL > inR){
    15         return NULL;
    16     }
    17     node* root = new node;
    18     root->data = post[postR];
    19     root->lev = level;
    20     int mid;
    21     for(mid = inL; mid <= inR; mid++){
    22         if(in[mid] == root->data)
    23             break;
    24     }
    25     int len = mid - inL;
    26     root->lchild = create(inL, mid - 1, postL, postL + len - 1, level + 1);
    27     root->rchild = create(mid + 1, inR, postL + len, postR - 1, level + 1);
    28     return root;
    29 }
    30 void levelOrder(node* root){
    31     queue<node*> Q;
    32     Q.push(root);
    33     int nowLev = root->lev;
    34     int reverTag = 1;
    35     int cnt = 0;
    36     vector<node*> vec;
    37     while(Q.empty() == false){
    38         node* temp = Q.front();
    39         Q.pop();
    40         if(temp->lev == nowLev){
    41             vec.push_back(temp);
    42         }else{
    43             nowLev = temp->lev;
    44             if(reverTag == 1){
    45                 reverTag = 0;
    46                 for(int i = vec.size() - 1; i>= 0; i--){
    47                     cnt++;
    48                     printf("%d ", vec[i]->data);
    49                 }
    50             }else{
    51                 reverTag = 1;
    52                 for(int i = 0; i < vec.size(); i++){
    53                     cnt++;
    54                     printf("%d ", vec[i]->data);
    55                 }
    56             }
    57             vec.clear();
    58             vec.push_back(temp);
    59         }
    60         if(temp->lchild != NULL)
    61             Q.push(temp->lchild);
    62         if(temp->rchild != NULL)
    63             Q.push(temp->rchild);
    64     }
    65     if(reverTag == 0){
    66         for(int i = 0; i < vec.size(); i++){
    67             cnt++;
    68             if(cnt == N)
    69                 printf("%d", vec[i]->data);
    70             else printf("%d ", vec[i]->data);
    71         }
    72     }else{
    73         for(int i = vec.size() - 1; i >= 0; i--){
    74             cnt++;
    75             if(cnt == N)
    76                 printf("%d", vec[i]->data);
    77             else printf("%d ", vec[i]->data);
    78         }
    79     }
    80 }
    81 int main(){
    82     scanf("%d", &N);
    83     for(int i = 1; i <= N; i++){
    84         scanf("%d", &in[i]);
    85     }
    86     for(int i = 1; i <= N; i++){
    87         scanf("%d",&post[i]);
    88     }
    89     node* root = create(1, N, 1, N, 1);
    90     levelOrder(root);
    91     cin >> N;
    92     return 0;
    93 }
    View Code

    总结:

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

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

  • 相关阅读:
    A Simple Problem About Truth Table
    Android Fragment完全解析,关于碎片你所需知道的一切
    Android系统中标准Intent的使用
    Android应用开发全程实录-你有多熟悉listView
    Android四大基本组件介绍及生命周期
    设计模式
    Java流(Stream)、文件(File)和IO
    Java网络编程详解
    Java 多线程
    深入理解Java虚拟机 精华总结(面试)
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8584675.html
Copyright © 2011-2022 走看看