15_3 静态二叉树的层序遍历

题目描述:

输出树的层次遍历的奇数层的所有结点。
从input_3.txt 输入

输入格式:

A B C//非叶子结点与其孩子
B E
C F G

输出格式:

1 层结点:A
第3 层结点:E,F,G

知识点

使用scanf记录一行字符串,以空格获得换行符为结束标记,当吸收一行字符后,要用getchar()吸收换行符‘\n’
gets()记录一行字符串之后,不需要getchar()吸收换行符‘\n’;

AC代码

#include <stdio.h>
#include <queue>
#include <vector>
#define maxn 26
using namespace std;

vector<int>T[maxn];
int level[26]={0};

void LevelOrder(int root)
{
    queue<int>q;
    vector<int>odd;
    q.push(root);
    level[root]=1;//
    int k=1;
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        if(level[temp]%2==1)
        {
            odd.push_back(temp);
        }
        else if(level[temp]==2*k)
        {
            printf("第%d层的结点有:",2*k-1);
            for(int i=0;i<(int)odd.size();i++)
            {
                printf("%c",odd[i]+'A');
                if(i<(int)odd.size()-1)printf(",");
                else printf("\n");
            }
            k++;
            odd.clear();

        }
        for(int i=0;i<(int)T[temp].size();i++)
        {
            int child=T[temp][i];
            q.push(child);
            level[child]=level[temp]+1;
        }
    }
 //如果只有奇数层,那么要加上下面这段代码,否则会少输出一层!
    if(odd.size()!=0)
    {
        printf("第%d层的结点有:",2*k-1);
        for(int i=0;i<(int)odd.size();i++)
        {
            printf("%c",odd[i]+'A');
            if(i<(int)odd.size()-1)printf(",");
            else printf("\n");
        }
        odd.clear();
        k++;
    }

}

int main()
{
    freopen("input.txt","r",stdin);
    char c;
    while(1)
    {
        int flag=0,a,b;
        while(c=getchar(),c!='\n'&&c!=EOF)
        {
            if(flag==0&&c>='A'&&c<='Z')
            {
                a=c-'A';
                flag=1;
            }
            else if(flag==1&&c>='A'&&c<='Z')
            {
                b=c-'A';
                T[a].push_back(b);
            }
        }
        if(c==EOF)break;
    }
    LevelOrder(0);
}

**

5月1日代码

**

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <time.h>
#include <queue>
#include <ctype.h>
using namespace std;

typedef struct Node
{
    int data,level;
    vector<int>child;
}Node;

Node Tree[200];
vector<int>level[100];

void levelorder(int root)
{
    queue<int>q;
    q.push(root);
    Tree[root].level=1;
    while(!q.empty())
    {
        int temp=q.front();
        int x=Tree[temp].level;
        level[x].push_back(temp);//
        q.pop();
        for(int i=0;i<Tree[temp].child.size();i++)
        {
            int u=Tree[temp].child[i];
            Tree[u].level=Tree[temp].level+1;
            q.push(u);
        }
    }
}

void to_print()
{
    for(int i=1;i<=100;i+=2)
    {
        if(level[i].size()==0)break;
        printf("第%d层结点:",i);
        for(int j=0;j<level[i].size();j++)
        {
            printf("%c",level[i][j]);
            if(j<level[i].size()-1)printf(",");
            else printf("\n");
        }
    }
}


int main()
{
    freopen("input.txt","r",stdin);
    int mark=0;
    char root;
    while(1)
    {
        int flag=0;
        char c,sub_root;
        while(1)
        {
            c=getchar();// 需要
            if(isalpha(c)&&flag==0)
            {
                if(mark==0)
                {
                    root=c;
                    mark=1;
                }
                sub_root=c;
                flag=1;
            }
            else if(isalpha(c))
            {
                Tree[sub_root].child.push_back(c);
            }
            else if(c=='\n'||c==EOF)break;
        }
        if(c==EOF)break;
    }
    levelorder(root);
    to_print();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值