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

    /* ***********************************************
    Author        :xryz
    Email         :523689985@qq.com
    Created Time  :4-22 11:41:40
    File Name     :BinaryTreeTraversals.cpp
    ************************************************ */
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    typedef struct node *tree;
    typedef struct node
    {
        int data;
        tree left;
        tree right;
    };
    
    bool flag;
    tree root;
    int pro[1024],in[1024];
    
    tree gettree(int *a,int *b,int n)//递归建树
    {
        tree t;
        int i;
        for(i=0;i<n;i++)
        {
            if(a[0]==b[i])
            {
                t=(tree)malloc(sizeof(struct node));
                t->data=b[i];
                t->left=gettree(a+1,b,i);
                t->right=gettree(a+1+i,b+1+i,n-i-1);
                return t;
            }
        }
        return NULL;
    }
    
    void postorder(tree p)//后序遍历
    {
        if(p!=NULL)
        {
            postorder(p->left);
            postorder(p->right);
            if(flag) printf(" ");
            else flag++;
            printf("%d",p->data);
        }
    }
    
    int main()
    {
        int i,n;
        while(~scanf("%d",&n))
        {
            for(i=0;i<n;i++)
                scanf("%d",&pro[i]);
            for(i=0;i<n;i++)
                scanf("%d",&in[i]);
            root=gettree(pro,in,n);
            flag=0;
            postorder(root);
            printf("
    ");
        }
        return 0;
    }
    
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    Golang Struct 声明和使用
    docker 中ulimit设置理解
    微服务架构引入的问题及解决方案
    Jenkins 集成Sonar代码质量扫描
    Jenkins和gitlab集成自动构建
    初识微服务架构
    jenkins 集成钉钉机器人通知
    Go 新起点
    shell中的(),{}几种语法用法
    二进制日志配置和运维管理
  • 原文地址:https://www.cnblogs.com/xryz/p/4848031.html
Copyright © 2011-2022 走看看