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左右,后来都跑去 ...
随机推荐
- Poj3074-Sudoku(数独DLX)
题意: 给出一个9*9的矩阵,有一些格子已经填了数,有一些是.代表未填.求任意一组解使得每行包含1~9,每列包含1~9,每个小矩形(3*3)包含1~9. 解析: 精确覆盖DLX的经典题目,每一行代表要 ...
- Maven中pom.xml常用元素说明
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- [转]Binarized Neural Networks_ Training Neural Networks with Weights and Activations Constrained to +1 or −1
原文: 二值神经网络(Binary Neural Network,BNN) 在我刚刚过去的研究生毕设中,我在ImageNet数据集上验证了图像特征二值化后仍然具有很强的表达能力,可以在检索中达到较好的 ...
- c#调用Excel绘制图表
c#调用Excel需要引用命名空间 using Microsoft.Office.Interop.Excel; 由于该程序不复杂,主要是根据不同数据画表和图,画的图像也并不复杂,因为画图和画表的操作会 ...
- 跨域资源共享 CORS 详解
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...
- JAVA读、写EXCEL文件
採用jxl.jar包,网上下载,百度一下到出都是.希望能够帮助到大家. 接下来直接贴代码: <span style="font-size:18px;"> public ...
- vs2013中国集
在TOOLS的菜单条下的最后一项.进去后在输入框输入Language.按Enter.选择语言,然后确定就可以. 如图 假设没有点击下拉框底下的链接 就会调挑转到语言包下载界面 下载须要的语言就可以, ...
- ACM-简单题之Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- [Spring入门学习笔记][静态资源]
遗留问题 在上一节课的作业中,我们一定遇到了一点问题——虽然将页面内容正确的返回给了浏览器,但是浏览器显示的样式却是不正确的,这是因为在HTML的\标签中我们这样引入了CSS资源: <link ...
- 失效的URL访问限制(转)
* 经常URL的保护仅仅是连接到该页面的链接不出现在未授权的用户面前.然而,一个有动机的.熟练的或者仅仅是幸运的黑客可能会找到并访问这些网页 , 调用这些功能并查看数据.在应用程序中,通过隐匿来实现安 ...