zoukankan      html  css  js  c++  java
  • hdu1710(Binary Tree Traversals)

    Binary Tree Traversals

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2803    Accepted Submission(s): 1242


    Problem Description
    A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

    In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

    In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

    In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

    Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
     
    Input
    The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
     
    Output
    For each test case print a single line specifying the corresponding postorder sequence.
     
    Sample Input
    9 1 2 4 7 3 5 8 9 6 4 7 2 1 8 5 9 3 6
     
    Sample Output
    7 4 2 8 9 5 6 3 1
     
    Source
    已知:前序和中序。输出后序
     
     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 struct tree
     5 {
     6     int num;
     7     tree *l,*r;
     8 };
     9 int T1[1005],T2[1005];
    10 int n;
    11 void init()
    12 {
    13     int i;
    14     for(i=0; i<n; i++)
    15         scanf("%d",&T1[i]);
    16     for(i=0; i<n; i++)
    17         scanf("%d",&T2[i]);
    18 }
    19 int Judge(int x,int y)
    20 {
    21     int i;
    22     for(i=0; i<n; i++)
    23     {
    24         if(x==T2[i])
    25             return 1;
    26         if(y==T2[i])
    27             return 0;
    28     }
    29     return 1;
    30 }
    31 
    32 void out(tree *t)//后序遍历
    33 {
    34     if(t->l)
    35         out(t->l);
    36     if(t->r)
    37         out(t->r);
    38     T1[n++]=t->num;
    39 }
    40 void BuildTree(tree *&t,int x)//建树
    41 {
    42     if(t==NULL)
    43     {
    44         t=new tree();
    45         t->num=x;
    46         t->l=NULL;
    47         t->r=NULL;
    48     }
    49     else
    50     {
    51         if(Judge(x,t->num))
    52         {
    53             BuildTree(t->l,x);
    54         }
    55         else
    56         {
    57             BuildTree(t->r,x);
    58         }
    59     }
    60 }
    61 void make()
    62 {
    63     tree *root=NULL;
    64     int i;
    65     for(i=0; i<n; i++)
    66     {
    67         BuildTree(root,T1[i]);
    68     }
    69     n=0;
    70     out(root);
    71     for(i=0; i<n-1; i++)
    72         printf("%d ",T1[i]);
    73     printf("%d
    ",T1[i]);
    74 }
    75 int main()
    76 {
    77     while(scanf("%d",&n)!=EOF)
    78     {
    79         init();
    80         make();
    81     }
    82     return 0;
    83 }
    View Code
  • 相关阅读:
    第0次作业(第二学期)
    C语言程序设计(基础)最后一次作业-- 总结报告
    第14、15教学周作业
    第七周作业
    第六周作业
    第四周作业
    第四次作业
    2018第三次作业
    2018第二次作业
    2018第三,四作业合集
  • 原文地址:https://www.cnblogs.com/sxmcACM/p/3491206.html
Copyright © 2011-2022 走看看