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. (2)基本工作流(制作场景与UI)

    1.认识cocos编辑器主界面.   2.制作新场景:   1)打开我们新建的HelloCocos项目,新建场景名称为“Scene”,如下图:   2)点击新建,场景就被创建出来了,如下图:   3) ...

  2. Smarty 函数

    html_checkboxes 自定义函数 html_checkboxes 根据给定的数据创建复选按钮组. 该函数可以指定哪些元素被选定. 要么必须指定 values 和 ouput 属性,要么指定 ...

  3. 20172305 2018-2019-1 《Java软件结构与数据结构》第二周学习总结

    20172305 2018-2019-1 <Java软件结构与数据结构>第二周学习总结 教材学习内容总结 本周内容主要为书第三章和第四章的内容: 第三章(以数组来替代栈的作用) 集合(聚集 ...

  4. git基本配置及使用

    目录 设置git git remote git-flow git merge 与 git rebase 参考 Git是分布式的代码管理工具,远程的代码管理是基于SSH的,所以要使用远程的Git则需要S ...

  5. Python3基础 判断变量大于一个数并且小于另外一个数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. 编译 Tensorflow 1.10 + CUDA9.2 + MKL【转】

    本文转自:https://www.solarck.com/compile-tensorflow-gpu.html 我的电脑系统是基于 Archlinux 的 Manjaro,软件包更新的比较激进,很早 ...

  7. linux下如何退出tmux和重新进入tmux

    1.退出(detach)当前tmux ctrl+d 2.重新进入tmux tmux attach -t <target-session> 如:当前有很多session,那么选择哪一个呢? ...

  8. ASP.NET WEB API 2: HTTP MESSAGE LIFECYLE

    https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf 1.You can host Web API inside IIS or ins ...

  9. 谈谈java中对象的深拷贝与浅拷贝

    知识点:java中关于Object.clone方法,对象的深拷贝与浅拷贝 引言: 在一些场景中,我们需要获取到一个对象的拷贝,这时候就可以用java中的Object.clone方法进行对象的复制,得到 ...

  10. c++ 计算指定半径圆的面积

    #include <iostream> #define PI 3.14 using namespace std; class Circle { float radius; public: ...