栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

R6-11 层次输出第n个结点 (6 分)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

R6-11 层次输出第n个结点 (6 分)

本题要求实现对于给定的二叉树,打印层次遍历序列中指定序号的结点。

函数接口定义:
 

void PrintNode(BiTree T,int n);

T是二叉树树根指针,PrintNode函数输出给定二叉树的层次遍历序列中第n个结点,n为结点在层次遍历序列中的序号,从1开始编号。

其中BinTree结构定义如下:

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

裁判测试程序样例:
 
#include  #include  typedef char ElemType; typedef struct BiTNode { ElemType data; struct BiTNode *lchild, *rchild; }BiTNode, *BiTree; BiTree Create(); void PrintNode(BiTree T,int n); int main() { BiTree T = Create(); int n; scanf("%d", &n); printf("The %d-th node in levelorder is: ", n); PrintNode(T,n); printf("n"); return 0; } 

输入样例:

输入第一行给出由字母和'#'组成的字符串,代表二叉树的扩展先序序列,第二行给出要打印的结点在后序序列中的序号n。例如对于如下二叉树,输入数据:

AB#DF##G##C##
2

输出样例(对于图中给出的树,当输入的n为2时):
The 2-th node in levelorder is: B
void PrintNode(BiTree T,int n)
{
     BiTree q[100];
    int h=0,r=0;
    if(T)
        q[r++]=T; 
        int cnt=0;
        while(h!=r){
          BiTree p=q[h++];
            cnt++;
           if(n==cnt) {printf("%c",p->data);return;}
            if(p->lchild)     q[r++]=p->lchild;
            if(p->rchild)    q[r++]=p->rchild;
        } 
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835408.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号