BFS visit tree
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的更多相关文章
- leetcode 690. Employee Importance——本质上就是tree的DFS和BFS
You are given a data structure of employee information, which includes the employee's unique id, his ...
- *[hackerrank]Tree Covering
https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完 ...
- Binary Tree Level Order Traversal 解答
Question Given a binary tree, return the level order traversal of its nodes' values. (ie, from left ...
- BFS与DFS常考算法整理
BFS与DFS常考算法整理 Preface BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或 ...
- 樹的DFS和BFS
菜鸟心得.... 不对请指出....... /*BFS,广度优先搜索树,用最简单的2叉树来举例, 树的结构如下: A B C D E F GH I J K L M N O广度优先搜索树, 顺序应该是A ...
- 【LeetCode OJ】Minimum Depth of Binary Tree
Problem Link: http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ To find the minimum dept ...
- Summer training #9
A:树形DP 给出一棵树,但是它的边是有向边,选择一个城市,问最少调整多少条边的方向能使一个选中城市可以到达所有的点,输出最小的调整的边数,和对应的点 要改变的边的权值为1,不需要改变的边的权值为0, ...
- [SHTSC 2007] 善意的投票
我就是来复习Dinic算法的,仅10天不写,我已经退化成写一遍+调试需要接近一个小时了,当然其中不乏在网上乱逛的时间… 赞成从S源点连一条单向边,反对向T汇点连一条单向边,朋友关系连双向边. 但是总感 ...
- [NOI 2006] 最大获利 80分
最后两点怎么搞都要30s+,但是我不会什么优化啊…暂时就这样吧.Dinic的时间复杂度是O(N^2*M) 这题和TDL的幼儿园模板是一样的. 这次写网络流给自己计时了,大约是40min左右,后来都跑去 ...
随机推荐
- VM虚拟机安装苹果雪豹操作系统
1.win xp虚拟机安装Mac OSX 一.用VM8安装mac os x10.6 ,然后升级到的10.6.8,如何安装vm大家自己百度吧.这里指列出了如何安装雪豹操作系统. DMG是mac os x ...
- HTTP缓存 1.0 vs 1.1
在“使用ETag跟踪用户”中有一点被忽略了,因为要用这张小图统计统计uv, 所以要求浏览器必须每次都要发送这个图片的请求.这需要服务器对图片的缓存策略做设置. http/1.0 和 http/1.1 ...
- Objective-C中NSString和NSMutableString的基本用法
int main(int argc, const char * argv[]) { @autoreleasepool { //----------------NSString------------- ...
- hdu 4007 Dave(线性探查+枚举)
Problem Description Recently, Dave is boring, so he often walks around. He finds that some places ar ...
- 简易的C/S系统(实现两个数的和)
//Client:#include <string.h> #include <sys/socket.h> #include <stdio.h> #include & ...
- Timer.3 - Binding arguments to a handler
In this tutorial we will modify the program from tutorial Timer.2 so that the timer fires once a sec ...
- PHP获取中英文混合字符串长度及截取
1.字符串长度 PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改 /** * PHP获取字符串中英文混合长度 * @param $str string 字符串 * ...
- Android学习笔记--JNI的使用方法
1.JNI是什么 JNI是Java Native Interface的缩写,它提供若干的API实现Java与其他语言之间的通信.而Android Framework由基于Java语言的的Java层与基 ...
- python os模块文件相关
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...
- windows2008无线网卡和.net3.5安装
今天在联想T420S笔记本上安装windows2008标准版,安装完成后部分驱动软件不能安装,要求.net framework3.5,下载.net3.5安装时提示应该用角色管理器安装. 根据提示打开服 ...