zoukankan      html  css  js  c++  java
  • HDOJ 1710 Binary Tree Traversals

    Binary Tree Traversals

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


    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
     
    Recommend
    lcy
     
    二叉树的3种遍历,因为没看清是多样例输出,WA了很久很久。。。
    很标准的做法是:先重建树,再按题目要求遍历树
     
     
     1 #include <iostream>
     2 #include <vector>
     3 //#include <fstream>
     4 
     5 
     6 using namespace std;
     7 
     8 
     9 int pre[1100];
    10 int mid[1100];
    11 
    12 
    13 vector<int> v;
    14 
    15 
    16 int n;
    17 
    18 
    19 struct Note
    20 {
    21     int key;
    22     int ch[2];
    23 }Tree[1100];
    24 
    25 
    26 int cur=0;
    27 
    28 
    29 int build(int pb,int mb,int n)
    30 {
    31     int i,k;
    32     if(n==0)  return -1;
    33     k=cur++;
    34     Tree[k].key=pre[pb];
    35     for(i=0;i<n&&pre[pb]!=mid[mb+i];i++) ;
    36 
    37 
    38     Tree[k].ch[0]=build(pb+1,mb,i);
    39     Tree[k].ch[1]=build(pb+i+1,mb+i+1,n-1-i);
    40 
    41 
    42     return k;
    43 }
    44 
    45 
    46 void print(int k,int l,int r)
    47 {
    48     if(l!=-1)
    49         print(l,Tree[l].ch[0],Tree[l].ch[1]);
    50     if(r!=-1)
    51         print(r,Tree[r].ch[0],Tree[r].ch[1]);
    52 
    53 
    54     v.push_back(Tree[k].key);
    55 }
    56 
    57 
    58 int main()
    59 {
    60  //   ifstream fin;
    61  //  fin.open("ssf.txt");
    62 
    63 
    64 
    65 
    66 while(cin>>n)
    67 {
    68         cur=0;
    69     for(int i=0;i<n;i++)
    70     {
    71         cin>>pre[i];
    72     }
    73     for(int i=0;i<n;i++)
    74     {
    75         cin>>mid[i];
    76     }
    77 
    78 
    79     build(0,0,n);
    80 
    81     print(0,Tree[0].ch[0],Tree[0].ch[1]);
    82 
    83 
    84     for(int u=0;u<n;u++)
    85     {
    86         if(u!=0) cout<<" ";
    87         cout<<v.at(u);
    88     }
    89     cout<<endl;
    90 
    91     while(!v.empty())
    92         v.pop_back();
    93 
    94 }
    95     return 0;
    96 }
  • 相关阅读:
    Python实例---抽屉后台框架分析
    Python学习---Django的验证码
    Python学习---DjangoForm的总结大全
    思维导图---思维导图网站
    编码学习---代码OJ网站
    multiple definition of XXX情况分析
    Qt 之 pro 配置详解
    萨尔曼可汗 数学视频
    Android sendevent/getevent 用法
    inux 驱动程序开发中输入子系统总共能产生哪些事件类型(EV_KEY,EV_ABS,EV_REL)
  • 原文地址:https://www.cnblogs.com/CKboss/p/3034806.html
Copyright © 2011-2022 走看看