• pat1086. Tree Traversals Again (25)


    1086. Tree Traversals Again (25)

    时间限制
    200 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


    Figure 1

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

    Output Specification:

    For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

    Sample Input:
    6
    Push 1
    Push 2
    Push 3
    Pop
    Pop
    Push 4
    Pop
    Pop
    Push 5
    Push 6
    Pop
    Pop
    
    Sample Output:
    3 4 2 6 5 1
    

    提交代码

     1 #include<cstdio>
     2 #include<stack>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<stack>
     6 #include<set>
     7 #include<map>
     8 #include<vector>
     9 using namespace std;
    10 int per[35],in[35];
    11 stack<int> s;
    12 int n;
    13 void Build(int *per,int *in,int num){
    14     if(num==0){
    15         return;
    16     }
    17     int mid=per[0];
    18 
    19     //cout<<mid<<endl;
    20 
    21     int i;
    22     for(i=0;i<num;i++){
    23         if(in[i]==mid){
    24             break;
    25         }
    26     }
    27 
    28     //cout<<num<<endl;
    29 
    30     Build(per+1,in,i);
    31     Build(per+1+i,in+1+i,num-1-i);
    32     if(num==n){
    33         printf("%d",mid);
    34     }
    35     else{
    36         printf("%d ",mid);
    37     }
    38 }
    39 int main(){
    40     //freopen("D:\INPUT.txt","r",stdin);
    41     scanf("%d",&n);
    42     n*=2;
    43     int i,num,pi=0,ini=0;
    44     string op;
    45     for(i=0;i<n;i++){
    46         cin>>op;
    47         if(op=="Push"){
    48             scanf("%d",&num);
    49             s.push(num);
    50             per[pi++]=num;
    51         }
    52         else{
    53             in[ini++]=s.top();
    54             s.pop();
    55         }
    56     }
    57     n=n/2;
    58 
    59     /*cout<<n<<endl;
    60     for(i=0;i<n;i++){
    61         cout<<per[i]<<" ";
    62     }
    63     cout<<endl;
    64     for(i=0;i<n;i++){
    65         cout<<in[i]<<" ";
    66     }
    67     cout<<endl;*/
    68 
    69     Build(per,in,n);
    70     printf("
    ");
    71     return 0;
    72 }
  • 相关阅读:
    URL 中使用 Base64 编码
    一个简单实用的C#日志类(第二版)
    C# 对象XML序列化
    VS2010安装项目的系统必备中添加.NET 2.0
    .NET Framework 4 安装程序
    使用ExeConfigurationFileMap读写配置文件
    C# 生成 XML
    Bug管理工具和测试管理工具介绍
    Android学习系列(19)App离线下载
    Android拓展系列(5)CyanogenMod源码下载和编译(Android ROM定制基础篇)
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4784479.html
Copyright © 2020-2023  润新知