• 1020. Tree Traversals (25)


    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

    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 postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in one line the level order traversal sequence of the corresponding binary tree. 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:

    7
    2 3 1 5 7 6 4
    1 2 3 4 5 6 7
    

    Sample Output:

    4 1 6 3 5 7 2
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 typedef struct TNode *Ptr;
     5 typedef Ptr Bintree;
     6 struct TNode
     7 {
     8     int data;
     9     Bintree left;
    10     Bintree right;
    11 };
    12 int inorder[50]; //中序
    13 int post[50];   //后序
    14 void Leverorder( Bintree tree);
    15 Bintree Create( int in[], int post[],int n);
    16 int main()
    17 {
    18     int n;
    19     int i;
    20     Bintree tree;
    21     scanf("%d",&n);
    22     for( i=0; i<n; i++)
    23         scanf("%d",&post[i]);
    24     for( i=0; i<n; i++)
    25         scanf("%d",&inorder[i]);
    26     tree = Create( inorder,post,n);
    27     Leverorder( tree );
    28     return 0;
    29 }
    30 
    31 Bintree Create( int in[], int post[],int n)
    32 {
    33     if( n==0 ) return NULL;
    34     int index=0;
    35     Bintree temp;
    36     temp = ( Bintree)malloc( sizeof(struct TNode));
    37     while( index<n )
    38     {
    39         if( in[index]== post[n-1])
    40             break;
    41         index++;
    42     }
    43     temp->data = post[n-1];
    44     temp->left = Create( in,post,index);
    45     temp->right = Create( in+index+1,post+index,n-index-1);
    46     return temp;
    47 }
    48 
    49 void Leverorder( Bintree tree)
    50 {
    51     if( tree== NULL)
    52         return ;
    53     Bintree T[50];
    54     int i=0,j=0;
    55     int flag=0;
    56     T[j++] = tree;
    57     while( i<j )
    58     {
    59         Bintree s = T[i];
    60         if( flag==0 )
    61             flag=1;
    62         else printf(" ");
    63         printf("%d",s->data);
    64         if( s->left )
    65             T[j++]=s->left;
    66         if( s->right)
    67             T[j++]=s->right;
    68         i++;
    69     }
    70 }
    在这个国度中,必须不停地奔跑,才能使你保持在原地。如果想要寻求突破,就要以两倍现在速度奔跑!
  • 相关阅读:
    重启Linux机器异常的解决方法
    SSH连接Linux服务器异常
    H5本地存储
    Spring MVC概述
    Linux下oracle开机自启动
    [ASP.NET MVC]@Partial 和@RenderPartial的区别
    [ASP.NET MVC]EntityFramework离线部署
    [ASP.NET MVC]@RenderSection,@RenderBody(),@RenderPage
    [ASP.NET MVC]@Html.AntiForgeryToken() 防止CSRF攻击
    [ASP.NET MVC]@Scripts.Render、@Styles.Render的使用
  • 原文地址:https://www.cnblogs.com/yuxiaoba/p/8578231.html
Copyright © 2020-2023  润新知