zoukankan      html  css  js  c++  java
  • 二叉树的输入

    链接:http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=2746

    Description

    用二叉树的带虚结点表示的前序遍历序可以唯一的确定一棵二叉树。

    Input

    输入包含多组数据。
    每行是一棵二叉树的带虚结点(#)表示的前序遍历序串,长度不超过2000。每个结点为一个字符。

    Output

    对每行输入,输出对应二叉树的中序遍历序(不含虚结点)、后序遍历序(不含虚结点)和层次遍历序(不含虚结点)。

     
    每棵二叉树的输出占一行,中序遍历序、后序遍历序和层次遍历序之间用一个空格隔开。

    Sample Input

    ab##c##
    #
    ab###
    Sample Output

    bac bca abc
    ba ba ab

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 #include<string.h>
      4 #include<queue>
      5 #include <iostream>
      6 using namespace std;
      7 typedef struct node
      8 {
      9     char data;
     10     struct node*lchild,*rchild;
     11 } pjq;
     12 char ch;
     13 void creat(pjq **p,int a)//先序建树
     14 {
     15     *p=NULL;
     16     if(a)
     17         ch=getchar();
     18     else
     19         a=1;
     20     if(ch=='
    ')
     21         return;
     22     else
     23     {
     24         if(ch=='#')
     25             *p=NULL;
     26         else
     27         {
     28             *p=(pjq *)malloc(sizeof(pjq));
     29             (*p)->data=ch;
     30             creat(&((*p)->lchild),a);
     31             creat(&((*p)->rchild),a);
     32         }
     33     }
     34 }
     35 void print1(pjq *p)//中序输出树
     36 {
     37     if(p!=NULL)
     38     {
     39         print1(p->lchild);
     40         printf("%c",p->data);
     41         print1(p->rchild);
     42     }
     43 }
     44 void print2(pjq *p)//后序输出树
     45 {
     46     if(p!=NULL)
     47     {
     48         print2(p->lchild);
     49         print2(p->rchild);
     50         printf("%c",p->data);
     51     }
     52 }
     53 void print3(pjq *b)//层次遍历输出树
     54 {
     55     if(b==NULL)
     56         return ;
     57     pjq *p;
     58     pjq *q[2050];
     59     int front,last;
     60     front=last=-1;
     61     last++;
     62     q[last]=b;
     63     while(front!=last)
     64     {
     65         front=(front+1);
     66         p=q[front];
     67         printf("%c",p->data);
     68         if(p->lchild!=NULL)
     69         {
     70             last++;
     71             q[last]=p->lchild;
     72         }
     73         if(p->rchild!=NULL)
     74         {
     75             last++;
     76             q[last]=p->rchild;
     77         }
     78     }
     79 }
     80 int main()
     81 {
     82     pjq *p=NULL;
     83     while((ch=getchar())!=EOF)
     84     {
     85         if(ch=='#')
     86             printf("
    ");
     87         else
     88         {
     89             int a=0;
     90             creat(&p,a);
     91             print1(p);
     92             printf(" ");
     93             print2(p);
     94             printf(" ");
     95             print3(p);
     96             printf("
    ");
     97         }
     98         getchar();
     99     }
    100     return 0;
    101 }
  • 相关阅读:
    C#执行异步操作的几种方式
    spring boot swagger ui使用 nginx 部署后无法使用问题
    PHP支付宝接口RSA验证
    认证加密算法php hash_hmac和java hmacSha1的问题
    如何实现从 Redis 中订阅消息转发到 WebSocket 客户端
    ffmpeg安装
    nginx静态资源反向代理
    Redis基础知识 之——发布/订阅
    linux下安装git
    php实现无限级分类查询(递归、非递归)
  • 原文地址:https://www.cnblogs.com/PJQOOO/p/4096952.html
Copyright © 2011-2022 走看看