6-11 Level-order Traversal(25 分)
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 分)的更多相关文章
- 【遍历二叉树】06二叉树曲折(Z字形)层次遍历II【Binary Tree Zigzag Level Order Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的Z字形层次 ...
- 【遍历二叉树】05二叉树的层次遍历II【Binary Tree Level Order Traversal II】
就把vector改成用栈类存放层次遍历的一层的序列 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【遍历二叉树】04二叉树的层次遍历【Binary Tree Level Order Traversal】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的 ...
- [刷题] 102 Binary Tree Level Order Traversal
要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...
- leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 102. Binary Tree Level Order Traversal
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- [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 ...
- 102. Binary Tree Level Order Traversal 广度优先遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [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 ...
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
随机推荐
- Js中的filter()方法
/* filter()方法使用指定的函数测试所有元素,并创建一个包含所有通过测试的元素的新数组. filter()基本语法: arr.filter(callback[, thisArg]) filte ...
- py文件生成pyc
鼠标右键 在此处打开命令行 python -m compileall xxx.py可以对当前目录下的xxx.py文件生成pyc
- windows下注册tomcat服务以及设置jvm参数
注册服务: 1 >cd /d D:\Java\tomcat-7.0.57-Css\bin //进入目录 1 >service.bat install //注册服务,同理删除服务为 rem ...
- GreenOpenPaint的实现(六)图片的保存和打开
如果只是直接的图片保存和打开,是没有很多内容的.但是我这里,将EXIF的信息融入其中,使得图像处理的结果能够保存下来.这样就非常有价值意义了. 所有的操作都放在DOC中进行处理. 我之前已经对EXIF ...
- noip2017普及 兔纸游玩记
初中的最后一场比赛...就这样结束了吧...QAQ时间...真够快的qwq 应该是初中的最后一篇游记了吧,尽量写多点... 这是一篇,初三 老年菜兔的 noip2017 普及游玩记吧! DAY 0 ...
- C#SendMessage用法
C#SendMessage用法 分类: C#操作内存相关 2011-11-26 23:52 1255人阅读 评论(0) 收藏 举报 函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口 ...
- python 判断列表字符串元素首尾字符是否相同
def match_words(words): ctr = for word in words: and word[] == word[-]: ctr += return ctr print(matc ...
- Ubuntu终端常用的快捷键,光标移动到开始位置
光标操作,实用 Ctrl+a 光标移动到开始位置 Ctrl+e 光标移动到最末尾 删除 Ctrl+k 删除此处至末尾的所有内容 Ctrl+u 删除此处至开始的所有内容 删除单个 Ctrl+d 删除当前 ...
- 2018-2019-2 网络对抗技术 20165332 Exp1 PC平台逆向破解
2018-2019-2 网络对抗技术 20165332 Exp1 PC平台逆向破解 NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP汇编指令:执行到NOP指令时,CPU仅仅当做一个 ...
- Rails 5 Test Prescriptions 第4章 什么制造了伟大的测试
伴随着程序成长,测试变长,复杂性增加,如何更高效的写测试,对以后开发不会造成麻烦. 测试本身没发被测试,所以一定要清楚,可控.不要加循环,不要过于复杂的自动编程. Cost and Value 成本和 ...