zoukankan      html  css  js  c++  java
  • 1119 Pre- and Post-order Traversals

    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, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

    Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

    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 preorder 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, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. 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.

     

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <vector>
     4 using namespace std;
     5 const int inf = 999999;
     6 const int maxn = 35;
     7 
     8 int n;
     9 int pre[maxn],post[maxn];
    10 int Tree[maxn][2];
    11 int flag=true;
    12 int buildTree(int l1,int r1,int l2,int r2){
    13     if(r1<l1) return -1;
    14     int root=pre[l1];
    15     
    16     if(l1==r1){
    17         Tree[root][0]=-1;
    18         Tree[root][1]=-1;
    19         return root;
    20     }
    21     
    22     if(l1+1==r1){
    23         Tree[root][1]=pre[l1+1];
    24         Tree[root][0]=-1;
    25         flag=false;
    26         return root;
    27     }
    28     
    29     int i;
    30     for(i=l2;post[i]!=pre[l1+1];i++);
    31     Tree[root][0]=buildTree(l1+1, l1+1+i-l2, l2, i);
    32     Tree[root][1]=buildTree(l1+2+i-l2, r1, i+1, r2-1);
    33     return root;
    34 }
    35 int cnt=0;
    36 void inOrderTra(int v){
    37     if(v==-1){
    38         return;
    39     }
    40     inOrderTra(Tree[v][0]);
    41     if(cnt) printf(" ");
    42     printf("%d",v); cnt++;
    43     inOrderTra(Tree[v][1]);
    44 }
    45 
    46 int main(){
    47     fill(Tree[0], Tree[0]+maxn*2, -1);
    48     
    49     scanf("%d",&n);
    50     for(int i=0;i<n;i++){
    51         scanf("%d",&pre[i]);
    52     }
    53     for(int i=0;i<n;i++){
    54         scanf("%d",&post[i]);
    55     }
    56     
    57     int Root=buildTree(0,n-1,0,n-1);
    58     if(flag) printf("Yes
    ");
    59     else printf("No
    ");
    60     inOrderTra(Root);
    61     printf("
    ");
    62 }
  • 相关阅读:
    isinstance函数
    Django之ORM那些相关操作
    Django ~ 2
    Django ~ 1
    Django详解之models操作
    Django模板语言相关内容
    livevent的几个问题
    客户端,服务器发包走向
    关闭客户端连接的两种情况
    std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义
  • 原文地址:https://www.cnblogs.com/lokwongho/p/10015951.html
Copyright © 2011-2022 走看看