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. 为什么要用Zero-Copy机制?

    考虑这样一种常用的情形:你需要将静态内容(类似图片.文件)展示给用户.那么这个情形就意味着你需要先将静态内容从磁盘中拷贝出来放到一个内存buf中,然后将这个buf通过socket传输给用户,进而用户或 ...

  2. SQL: 拼接列

    1. 因工作需要,需把两列(id,created_by)拼接成一列,结果很有意思,前5个值都是null. 2.解决方法:null加减乘除任何值都等于null,所以使用isnull函数先处理下列的值再拼 ...

  3. 最佳虚拟容器LXC

    最佳虚拟容器LXC 和"真正的虚拟机环境"不同, "容器"(container)只能在Linux上虚拟Linux, 不能虚拟WIndows, 因为它不能虚拟硬件 ...

  4. springcloud13---zuul

    Zuul:API  GATEWAY (服务网关): http://blog.daocloud.io/microservices-2/ 一个客户端不同的功能请求不同的微服务,那么客户端要知道所有微服务的 ...

  5. UVaLive4992:Jungle Outpost

    传送门 半平面交. 首先,由显然成立法可以证明炸连续的几个总比分散火力效果更佳. 所以二分答案,转化为判定问题,即间隔$ans$个点的连线的半平面交是否为空. 半平面交判定即可. 时间复杂度:$O(N ...

  6. BZOj 墨墨的等式(转化为最短路)题解

    题意:中文题意不解释... 思路:这道题居然可以转化为最短路orz,要等式有非负整数解,我们可以转化一下:每个ai不限数量,问你能用ai数组拼出多少个Bmin~Bmax范围内的数,有点像完全背包的感觉 ...

  7. git commit的规范

    https://www.yuque.com/fe9/basic/nruxq8#6c228def 制定一个 git commit 信息的提交规范是开发团队工作流必不可少的环节.试想一下,如果查看主分支上 ...

  8. Python学习札记(二十七) 函数式编程8 偏函数

    偏函数 NOTE 1.int()函数提供额外的base参数,默认值为10.如果传入base参数,就可以做N进制的转换: #!/usr/bin/env python3 import functools ...

  9. POJ 3281 Dining(最大流)

    http://poj.org/problem?id=3281 题意: 有n头牛,F种食物和D种饮料,每头牛都有自己喜欢的食物和饮料,每种食物和饮料只能给一头牛,每头牛需要1食物和1饮料.问最多能满足几 ...

  10. python swap

    swap里面的a,b 不会影响函数作用域外面的变量 java也不可以的吧:python里面没有指针,你可以认为所有的东西都是指向的内容,但是不要试图去改变指针的值 其实我觉得所有的对象都是不可变对象, ...