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
  • 相关阅读:
    HTTP协议中GET、POST和HEAD的介绍
    Django model 字段类型清单
    MySQL的菜鸟级操作
    windows7下将Cygwin加入右键菜单,并从当前目录打开
    数组指针和指针数组的区别
    const引用与非const引用
    printf("33[1;33m ***** 33[0m ");
    C语言可变参数函数详解示例
    机顶盒demux的工作原理
    机顶盒的工作原理
  • 原文地址:https://www.cnblogs.com/dzzy/p/5067971.html
Copyright © 2011-2022 走看看