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("\n");
return 0;
}
/* 你的代码将被嵌在这里 */

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

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;
}
} }

PTA 二叉树的层次遍历的更多相关文章

  1. lintcode : 二叉树的层次遍历II

    题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, ...

  2. lintcode : 二叉树的层次遍历

    题目 二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历 ...

  3. LintCode 二叉树的层次遍历 II

    中等 二叉树的层次遍历 II 查看执行结果 42% 通过 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 您在真实的面试中是否遇到过这个 ...

  4. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  5. 【leetcode-102,107,103】 二叉树的层次遍历

    102. 二叉树的层次遍历 (1过,隐蔽错误花时间很多,简单题目本应很快,下次注意红色错误的地方) 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: ...

  6. 107. 二叉树的层次遍历 II

    107. 二叉树的层次遍历 II 题意 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历). 解题思路 递归:利用前序遍历的思想,在递归过程中 ...

  7. Leetcode 102 二叉树的层次遍历 Python

    二叉树的层次遍历 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7],   3   / \ 9 20 ...

  8. LeetCode 107 ——二叉树的层次遍历 II

    1. 题目 2. 解答 与 LeetCode 102 --二叉树的层次遍历 类似,我们只需要将每一层的数据倒序输出即可. 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level ...

  9. LintCode-70.二叉树的层次遍历 II

    二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 按照 ...

随机推荐

  1. cs实时系统之网关设计

    今天给大家讲一下client-server系统(cs)设计,基本结构 1.client 客户端,插件式开发,负责对应ui的展示 2.gateway 网关层,管理客户端通信连接,负载后端集群服务 3.s ...

  2. 如何使用 js 实现一个 throttle 函数

    如何使用 js 实现一个 throttle 函数 原理 实现方式 "use strict"; /** * * @author xgqfrms * @license MIT * @c ...

  3. where is the storage location of the browser's HTTP cache? disk or memory

    where is the storage location of the browser's HTTP cache? disk or memory HTTP cache & storage l ...

  4. Raspberry Pi & Raspberry Pi 4

    Raspberry Pi & Raspberry Pi 4 pdf https://www.raspberrypi.org/magpi/issues/beginners-guide-2nd-e ...

  5. SSR & 轮询登录 & Token

    SSR & 轮询登录 & Token https://yuchengkai.cn/docs/frontend 扫码登录原理 https://www.cnblogs.com/xgqfrm ...

  6. xcode upgrade & git bug

    xcode upgrade & git bug ➜ op-static git checkout feature/select-seat-system Agreeing to the Xcod ...

  7. 「NGK每日快讯」12.29日NGK第56期官方快讯!

  8. 微信附近的人,用redis也能实现?(GEO)

    相信微信附近的人的功能大家都应该用过 我可以很随意的通过我自己的定位能看到我附近的人,并且能看到那个人距离我的距离,大家有没有思考过这个是怎么实现的? 作为一个程序猿任何问题应该都有一个思考的过程,而 ...

  9. iOS 兼容性处理

    1. scroll滑动层,在iOS中滑动不流畅的处理 -webkit-overflow-scrolling:touch; //在滑动层标签添加这个样式 2. iOS 系统中input标签,去掉圆角效果 ...

  10. Scrapy项目_阳光热线问政平台

    目的: 爬取阳光热线问政平台问题中每个帖子的标题.详情URL.详情内容.图片以及发布时间 步骤: 1.创建爬虫项目 1 scrapy startproject yangguang 2 cd yangg ...