zoukankan      html  css  js  c++  java
  • HDU 1710 二叉树三种遍历

    Binary Tree Traversals

    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
     
    通过先序中序序列求出后序遍历序列,递归玩的不熟,树是时候系统学习一下了。
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int  num=0;
    struct donser
    {
        int data;
        donser*lson;
        donser*rson;
    };
    donser *root;
    donser *creat(int *a,int *b,int n)//da下标从i开始依次找  db下标从j开始向后扫  n向后扫几个
    {
        donser *ss;
        for(int k=0;k<n;k++)
        {
            if(b[k]==a[0])
            {
                ss=new donser;
                ss->data=b[k];
                ss->lson=creat(a+1,b,k);
                ss->rson=creat(a+k+1,b+1+k,n-k-1);
                return ss;
            }
        }
        return NULL;
    }
    
    void solve(donser *r)
    {
        if(r!=NULL)
        {
            solve(r->lson);
            solve(r->rson);
            if(r!=root) cout<<r->data<<" ";
            else cout<<r->data<<endl;
        }
    }
    
    int main()
    {
        int  da[1010],db[1010];//前中
        while(~scanf("%d",&num))
        {
            for(int i=0;i<num;i++) {scanf("%d",&da[i]);}
            for(int i=0;i<num;i++) {scanf("%d",&db[i]);}
            root=creat(da,db,num);
            solve(root);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    世界充满神秘的不平衡:创业中的“二八”法则
    树立个人品牌:从名字开始
    房子,心中的痛
    今生,谁会是我最美丽的新娘
    失败不是创业的结束,因为有了你们,这世界才璀璨(转载)
    再等五百年
    名字作诗:为您的名字增添一份色彩
    揭开爱情的外衣
    李子楠(帮客户名字作诗)
    爱上你,早已是命中注定
  • 原文地址:https://www.cnblogs.com/dzzy/p/5067971.html
Copyright © 2011-2022 走看看