zoukankan      html  css  js  c++  java
  • hdu1710-Binary Tree Traversals (由二叉树的先序序列和中序序列求后序序列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1710

    Binary Tree Traversals

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


    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
     

    题解:意思很直白。给你二叉树的先序序列和中序序列,让你求后序序列。先利用先序序列和中序序列的特征递归建树,再后序遍历。

    代码:

     1 #include <fstream>
     2 #include <iostream>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int N=1005;
     8 struct node{
     9     int c;
    10     struct node *lch,*rch;
    11 };
    12 int n,tn;
    13 int pre[N],in[N];
    14 
    15 void createTree(int* l,int* r,int i,int j,int e,int f,node** tree);
    16 void postOrder(node *p);
    17 
    18 int main(){
    19     //freopen("D:\input.in","r",stdin);
    20     //freopen("D:\output.out","w",stdout);
    21     node *tree;
    22     while(~scanf("%d",&n)){
    23         for(int i=0;i<n;i++)
    24             scanf("%d",&pre[i]);
    25         for(int i=0;i<n;i++)
    26             scanf("%d",&in[i]);
    27         createTree(pre,in,0,n-1,0,n-1,&tree);
    28         tn=0;
    29         postOrder(tree);
    30     }
    31     return 0;
    32 }
    33 void createTree(int* l,int* r,int i,int j,int e,int f,node** tree){//可以思考下这里为什么是**,可不可以用*?
    34     int m;
    35     (*tree)=new node;
    36     (*tree)->c=l[i];
    37     m=e;
    38     while(r[m]!=l[i])    m++;
    39     if(m==e)    (*tree)->lch=NULL;
    40     else    createTree(l,r,i+1,i+m-e,e,m-1,&(*tree)->lch);
    41     if(m==f)    (*tree)->rch=NULL;
    42     else    createTree(l,r,i+m-e+1,j,m+1,f,&(*tree)->rch);
    43 }
    44 void postOrder(node *p){
    45     if(p!=NULL){
    46         postOrder(p->lch);
    47         postOrder(p->rch);
    48         printf("%d",p->c);
    49         if(++tn<n)  printf(" ");
    50         else    puts("");
    51         delete p;
    52     }
    53 }
  • 相关阅读:
    Merlin 的魔力: SpringLayout 管理器
    setOpaque(true);设置控件不透明
    运用 BoxLayout 进行 Swing 控件布局
    李洪强iOS开发本人集成环信的经验总结_02_基本配置
    李洪强iOS开发之-PCH文件的配置
    李洪强iOS开发本人集成环信的经验总结_01环信SDK的导入
    iOS开发UI篇—Quartz2D使用(矩阵操作)
    iOS开发UI篇—Quartz2D使用(图形上下文栈)
    iOS开发UI篇—Quartz2D简单使用(二)
    iOS开发UI篇—Quartz2D简单使用(一)
  • 原文地址:https://www.cnblogs.com/jiu0821/p/4634663.html
Copyright © 2011-2022 走看看