Write a routine to list out the nodes of a binary tree in "level-order". List the root, then nodes at depth 1, followed by nodes at depth 2, and so on. You must do this in linear time.

Format of functions:

void Level_order ( Tree T, void (*visit)(Tree ThisNode) );

where void (*visit)(Tree ThisNode) is a function that handles ThisNode being visited by Level_order, and Tree is defined as the following:

typedef struct TreeNode *Tree;
struct TreeNode {
ElementType Element;
Tree Left;
Tree Right;
};

Sample program of judge:

#include <stdio.h>
#include <stdlib.h> #define MaxTree 10 /* maximum number of nodes in a tree */
typedef int ElementType; typedef struct TreeNode *Tree;
struct TreeNode {
ElementType Element;
Tree Left;
Tree Right;
}; Tree BuildTree(); /* details omitted */
void PrintNode( Tree NodePtr )
{
printf(" %d", NodePtr->Element);
} void Level_order ( Tree T, void (*visit)(Tree ThisNode) ); int main()
{
Tree T = BuildTree();
printf("Level-order:");
Level_order(T, PrintNode);
return 0;
} /* Your function will be put here */

Sample Output (for the tree shown in the figure):

Level-order: 3 5 6 1 8 10 9
代码:
void Level_order ( Tree T, void (*visit)(Tree ThisNode) )
{
Tree s[];
int head = ,tail = ;
if(T)s[tail ++] = T;
while(head < tail)
{
if(s[head] -> Left)s[tail ++] = s[head] -> Left;
if(s[head] -> Right)s[tail ++] = s[head] -> Right;
(*visit)(s[head ++]);
}
}

6-11 Level-order Traversal(25 分)的更多相关文章

  1. 【遍历二叉树】06二叉树曲折(Z字形)层次遍历II【Binary Tree Zigzag Level Order Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的Z字形层次 ...

  2. 【遍历二叉树】05二叉树的层次遍历II【Binary Tree Level Order Traversal II】

    就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

  3. 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...

  4. [刷题] 102 Binary Tree Level Order Traversal

    要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...

  5. leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

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

  6. 102. Binary Tree Level Order Traversal

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

  7. [Leetcode] Binary tree Zigzag level order traversal二叉树Z形层次遍历

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

  8. 102. Binary Tree Level Order Traversal 广度优先遍历

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

  9. [LC] 429. N-ary Tree Level Order Traversal

    Given an n-ary tree, return the level order traversal of its nodes' values. Nary-Tree input serializ ...

  10. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

随机推荐

  1. centos7 lua安装

    yum -y install gcc automake autoconf libtool makeyum install readline-develcurl -R -O http://www.lua ...

  2. 手把手教你学node.js之使用 superagent 与 cheerio 完成简单爬虫

    使用 superagent 与 cheerio 完成简单爬虫 目标 建立一个 lesson 3 项目,在其中编写代码. 当在浏览器中访问 http://localhost:3000/ 时,输出 CNo ...

  3. Tomcat java.lang.OutOfMemoryError: PermGen space error

    Often time, Tomcat may hits the following java.lang.OutOfMemoryError: PermGen space error. java.lang ...

  4. Vue学习笔记之Vue的模板字符串

    0x00 模板字符串 传统的JavaScript语言,输出模板通常是这样的写的. $('#result').append( 'There are <b>' + basket.count + ...

  5. Linux网络子系统之---- PHY 配置

    MII即媒体独立接口,也叫介质无关接口. 它包括一个数据接口,以及一个MAC和PHY之间的管理接口(图1). 数据接口包括分别用于发送器和接收器的两条独立信道.每条信道都有自己的数据.时钟和控制信号. ...

  6. k8s 学习笔记 etcd

    1. Etcd Etcd是Kubernetes集群中的一个十分重要的组件,用于保存集群所有的网络配置和对象的状态信息.在后面具体的安装环境中,我们安装的etcd的版本是v3.1.5,整个kuberne ...

  7. phantomjs学习资料

    http://blog.csdn.net/mecho/article/details/45888465 phantomjs的使用说明,尤其是webpage创建,资源加载前后的处理方法.

  8. error: device offline - waiting for device -

    解决方法:重启服务 一.关闭 adb kill-server 二.启动 adb start-server 三.连接 adb connect 192.168.1.10 四.查看设备 adb device ...

  9. iframe.contentWindow介绍

    一.在使用iframe的页面,要操作这个iframe里面的DOM元素可以用: contentWindow.contentDocument(测试的时候chrome浏览器,要在服务器环境下) 1.先获取i ...

  10. 004——php字符串中处理函数(三)

    <?php /** * 字符串替换函数: * str_replace(); 替换字符串或数组元素,区分大小写,第四个参数可选,用于统计替换次数 * str_ireplace()不区分大小写替换 ...