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 ...
随机推荐
- php之isset empty is_null的区别
isset:当前变量没有设置(即不存在),或者变量设置为null的时候,返回true,设置为“”或者0 都是返回的true empty:变量不存在,设置值为null,设置为“”,设置为0 都返回tru ...
- Linq Query常见错误
1.只能对 Type.IsGenericParameter 为 True 的类型调用方法 对于此错误,一般常见在虚拟实体,但是要把条件拼接在Expression中,通常是因为该字段在数据库中是可空的, ...
- 手把手教你学node.js之一个简单的express应用
一个简单的express应用 目标 建立一个 lesson1 项目,在其中编写代码.当在浏览器中访问 http://localhost:3000/ 时,输出 Hello World. 挑战 访问 ht ...
- Least slack time scheduling
This algorithm is also known as least laxity first. 词语解释:Laxity 松懈的:马虎的:不严格的,Least-Laxity-First 松弛程度 ...
- c++第二十二天
p120~p124: 表达式 1.表达式由一个或者多个运算对象组成. 2.最简单的表达式是字面值和变量. 3.一元运算符作用于一个运算对象,二元则作用于两个.一个运算符到底是几元由上下文决定. 4.重 ...
- [POI2006][luogu3435] OKR-Periods of Words [kmp+next数组]
题面 传送门 思路 先把题面转成人话: 对于给定串的每个前缀i,求最长的,使这个字符串重复两边能覆盖原前缀i的前缀(就是前缀i的一个前缀),求所有的这些"前缀的前缀"的长度和 利用 ...
- Egret引擎开发基础(一)
显示图片 var batman:egret.Bitmap = new egret.Bitmap( RES.getRes('hexo-huaheshang_png')); batman.x = 0; b ...
- leetcode刷题吧
排列 从排序的数组中删除重复项 /** * @param {number[]} nums * @return {number} */ var removeDuplicates = function(n ...
- 51Nod 1509 加长棒(隔板法)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1509 思路: 直接去解可行的方法有点麻烦,所以应该用总的方法去减去不可行 ...
- 图像等比例缩放的函数封装(PHP)
<?php //图像等比例缩放函数 /** *等比例缩放函数(以保存新图片的方式实现) *@param string $picname 被缩放的处理图片源 *@param int $maxx 缩 ...