zoukankan      html  css  js  c++  java
  • PTA 二叉树的层次遍历

    6-6 二叉树的层次遍历 (6 分)
     

    本题要求实现给定的二叉树的层次遍历。

    函数接口定义:

    
    void Levelorder(BiTree T);
    
    

    T是二叉树树根指针,Levelorder函数输出给定二叉树的层次遍历序列,格式为一个空格跟着一个字符。

    其中BinTree结构定义如下:

    typedef char ElemType;
    typedef struct BiTNode
    {
       ElemType data;
       struct BiTNode *lchild, *rchild;
    }BiTNode, *BiTree;
    

    裁判测试程序样例:

    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef char ElemType;
    typedef struct BiTNode
    {
       ElemType data;
       struct BiTNode *lchild, *rchild;
    }BiTNode, *BiTree;
    
    BiTree Create();/* 细节在此不表 */
    
    void Levelorder(BiTree T);
    
    int main()
    {
       BiTree T = Create();
       printf("Levelorder:"); Levelorder(T); printf("
    ");
       return 0;
    }
    /* 你的代码将被嵌在这里 */
    

    输出样例(对于图中给出的树):

    二叉树.png

    Levelorder: A B C D F G
    void Levelorder(BiTree T){
        int max=10;
        BiTree a[max];
        BiTree t=NULL;
        int front=0,rear=0;
        if(T!=NULL){
            a[rear]=T;
            rear=(rear+1)%max;
        }
        while(rear!=front){
           t=a[front];
           front=(front+1)%max;
           printf(" %c",t->data);
           if(t->lchild!=NULL){
               a[rear]=t->lchild;
               rear=(rear+1)%max;
           } 
           if(t->rchild!=NULL){
               a[rear]=t->rchild;
               rear=(rear+1)%max;
           }
        }    
        
        
        
    }
  • 相关阅读:
    Java基础知识:正则表达式
    NodeJs 中 将表单数据转发到后台
    单片机的远程升级
    一些开源协议
    物联网的一些例子
    python一些开源特色库
    qt练习
    网页编程学习笔记
    PCB相关
    工业控制系统
  • 原文地址:https://www.cnblogs.com/DirWang/p/11929999.html
Copyright © 2011-2022 走看看