zoukankan      html  css  js  c++  java
  • A1086. Tree Traversals Again (25)

    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 <stdio.h>
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <string.h>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <string>
     8 #include <stack> 
     9 #include <queue>
    10 using namespace std;
    11 const int maxn=33; 
    12  int n;
    13 int pre[maxn],in[maxn];
    14 struct node{
    15     int data;
    16     node *left;
    17     node *right;
    18 };
    19 //给一个先序和中序,求层次遍历
    20 node *create(int a,int b,int c,int d)
    21 {
    22       if(a>b)return NULL;
    23       node *temp=new node;
    24       temp->data=pre[a];
    25       int k=0;
    26       for(;k+c<=d;k++)
    27       {
    28         if(in[c+k]==pre[a])break;    
    29     }
    30     temp->left=create(a+1,a+k,c,c+k-1);
    31     temp->right=create(a+k+1,b,c+k+1,d);
    32     return temp;
    33 } 
    34 //后序
    35 int printcount=0;
    36 void postorder(node * root)
    37 {
    38     if(root==NULL)return;
    39     postorder(root->left);
    40     postorder(root->right);
    41     printf("%d",root->data);
    42     printcount++;
    43     if(printcount<n)printf(" ");
    44  } 
    45 void BFS(node * root)
    46 {
    47     int count=0;
    48   queue<node*> q;
    49   q.push(root);
    50   while(!q.empty())
    51   {
    52    node* now=q.front();
    53    q.pop();
    54    printf("%d",now->data);
    55    count++;
    56    if(count<n)printf(" ");
    57    if(now->left!=NULL)q.push(now->left);
    58    if(now->right!=NULL)q.push(now->right);
    59   }    
    60 }
    61 int main(){
    62     int temp,num=0,innum=0;
    63     stack<int> st;
    64     scanf("%d",&n);
    65     for(int i=0;i<n*2;i++)
    66     {
    67      char a[5];
    68      scanf("%s",a);
    69      if(strcmp(a,"Push")==0)
    70      {
    71          scanf("%d",&temp);
    72          pre[num++]=temp;
    73          st.push(temp);
    74      }//pop
    75      else
    76      {
    77          in[innum++]=st.top();
    78          st.pop();
    79      }
    80     }
    81     //根据前序中序 建树
    82     
    83     node* root=create(0,n-1,0,n-1);
    84     postorder(root); 
    85     return 0;
    86 }
  • 相关阅读:
    联合查询
    单表查询
    表和表之间的关系
    mysql完整性约束
    mysql 数据类型
    DRBD 数据镜像软件
    Memcached
    Redis
    SVN版本控制服务
    大数据hadoop分布式系统
  • 原文地址:https://www.cnblogs.com/ligen/p/4320746.html
Copyright © 2011-2022 走看看