There are two ways to conduct BFS on tree.

Solution 1 -- Given level

Use recursion to find given level, and print.

/*Function to print level order traversal of tree*/
printLevelorder(tree)
for d = 1 to height(tree)
printGivenLevel(tree, d); /*Function to print all nodes at a given level*/
printGivenLevel(tree, level)
if tree is NULL then return;
if level is 1, then
print(tree->data);
else if level greater than 1, then
printGivenLevel(tree->left, level-1);
printGivenLevel(tree->right, level-1);

Solution 2 -- Queue

For each node, first the node is visited and then it’s child nodes are put in a FIFO queue.

printLevelorder(tree)
1) Create an empty queue q
2) temp_node = root /*start from root*/
3) Loop while temp_node is not NULL
a) print temp_node->data.
b) Enqueue temp_node’s children (first left then right children) to q
c) Dequeue a node from q and assign it’s value to temp_node

Reference: Level Order Tree Traversal

BFS visit tree的更多相关文章

  1. leetcode 690. Employee Importance——本质上就是tree的DFS和BFS

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  2. *[hackerrank]Tree Covering

    https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完 ...

  3. Binary Tree Level Order Traversal 解答

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

  4. BFS与DFS常考算法整理

    BFS与DFS常考算法整理 Preface BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或 ...

  5. 樹的DFS和BFS

    菜鸟心得.... 不对请指出....... /*BFS,广度优先搜索树,用最简单的2叉树来举例, 树的结构如下: A B C D E F GH I J K L M N O广度优先搜索树, 顺序应该是A ...

  6. 【LeetCode OJ】Minimum Depth of Binary Tree

    Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...

  7. Summer training #9

    A:树形DP 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点 要改变的边的权值为1,不需要改变的边的权值为0, ...

  8. [SHTSC 2007] 善意的投票

    我就是来复习Dinic算法的,仅10天不写,我已经退化成写一遍+调试需要接近一个小时了,当然其中不乏在网上乱逛的时间… 赞成从S源点连一条单向边,反对向T汇点连一条单向边,朋友关系连双向边. 但是总感 ...

  9. [NOI 2006] 最大获利 80分

    最后两点怎么搞都要30s+,但是我不会什么优化啊…暂时就这样吧.Dinic的时间复杂度是O(N^2*M) 这题和TDL的幼儿园模板是一样的. 这次写网络流给自己计时了,大约是40min左右,后来都跑去 ...

随机推荐

  1. unix c 05

    dup和dup2用于复制文件描述符,区别在于dup2可以指定新的文件描述符的数值,如果新的文件描述符的值已经被使用,dup2会关闭掉后进行复制. dup和dup2 不会复制文件表,只是文件描述符的复制 ...

  2. Axure设计分析作业-实例解析

    本文转载自人人都谁产品经理,作者完全使用Axure做了这一个产品需求文档.文档地址:http://1passwordmanager.sinaapp.com/ 大家可以先睹为快.这个PRD完全使用axu ...

  3. Hessian和Burlap入门教程

    一.简介 Hessian和Burlap是由Caucho Technology提供的基于HTTP协议的轻量级远程服务解决方案.他们都致力于借助尽可能简单那的API和通信协议来简化Web服务.    He ...

  4. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)

    2.4.5 堆排序 我们可以把任意优先队列变成一种排序方法.将所有元素插入一个查找最小元素的有限队列,然后再重复调用删除最小元素的操作来将他们按顺序删去.用无序数组实现的优先队列这么做相当于进行一次插 ...

  5. <转载>模板声明中template <typename T>和template <class T>

    原文地址http://blog.csdn.net/bug07250432/article/details/10150625 在c++Template中很多地方都用到了typename与class这两个 ...

  6. vi常用命令集锦

    转载,原文地址:http://blog.csdn.net/hackbuteer1/article/details/6575232 vi编辑器有3种模式:命令模式.输入模式.末行模式.掌握这三种模式十分 ...

  7. 关于sem_unlink什么时候删除信号量

    sem_unlink在man手册里有这么一段话: sem_unlink() removes the named semaphore referred to by name. The semaphore ...

  8. 用Visual Studio2010 编译 C++文件"hello world”

    本周开始学习C++语言,用Visual Studio 2010做编译器,发现站内还没有基础的关于用VS2010编译程序的教材.而且自己在网上寻找时候,教程难找,而且大都不详细.故写一个关于这方面的教程 ...

  9. 又一道软通动力7K月薪面试题——银行业务调度系统

    后期补充:网友对我诟病最多的就是我帮学生做面试题,说这是小偷和骗子行为,在此,我对自己给学员做面试题做出例如以下解释:  (1)学员拿着面试题来找老师,学生也事先思考和尝试后实在没有办法,又求职心切才 ...

  10. 自制获取data-自定义属性

    jQuery.fn.dataset = function(attr, val) { // 获取数据集 if (arguments.length == 0) { var dataset = {}; jQ ...