CSharp Algorithm - How to traverse binary tree by breadth (Part II)
/*
Author: Jiangong SUN
*/
Here I will introduce the breadth first traversal of binary tree.
The principe is that you traverse the binary tree level by level.
This traversal is quite different than depth first traversal. In depth first traversal you can use recursive method to traverse.
Here is one solution using a queue.
/// <summary>
/// breadth-first traversal 宽度遍历树
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="node"></param>
public void Traversal<T>(Node<T> node)
{
//create a Node<T> queue
var treeQueue = new Queue<Node<T>>();
//initialize queue with tree root node
treeQueue.Enqueue(node); //if queue has elements
while (treeQueue.Count > 0)
{
//dequeue the queue's first node
Node<T> current = treeQueue.Dequeue(); //print the node name
PrintNodeName(current); //if node has Left leaf, enqueue it
if (current.LNode != null)
treeQueue.Enqueue(current.LNode); //if node has right leaf, enqueue it
if (current.RNode != null)
treeQueue.Enqueue(current.RNode);
}
}
references;
http://www.cs.bu.edu/teaching/c/tree/breadth-first/
http://hectorea.com/blog/programming-interview-31-binarytree-traversal-by-levels-breadth-first/#
CSharp Algorithm - How to traverse binary tree by breadth (Part II)的更多相关文章
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...
- LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
- 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...
- LeetCode_107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...
- 63. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...
- [Algorithm] Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- [Algorithm] Check if a binary tree is binary search tree or not
What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...
随机推荐
- mysql select count(filed) 问题(where条件没有数据匹配的话也有数据返回)。
问题: SELECT count(*),user_id FROM tb_rp_logintrace WHERE id=-1 返回结果: count(*), user_id 0 ...
- 校园网之MentoHUST安装与使用
作用:MentoHUST可以解决校园网锐捷客户端与Windows的兼容性问题,可以解决安装虚拟机之后虚拟机网卡与本地网卡冲突的问题,可以做到愉快的用校园网,并可以愉快的用校园网开Wifi给自己或者小伙 ...
- BZOJ_1007_ [HNOI2008]_水平可见直线_(单调栈+凸包)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1007 给出一些直线,沿着y轴从上往下看,能看到多少条直线. 分析 由于直线相交,会遮挡住一些直 ...
- [swustoj 411] 售货员的难题
售货员的难题(0411) Time limit(ms): 5000 Memory limit(kb): 65535 Submission: 1744 Accepted: 200 Description ...
- 去除浏览器下jquey easyui datagrid、combotree 缓存问题
在页面脚本中加入以下内容即可: $.ajaxSetup ({ cache: false //关闭AJAX相应的缓存 });
- HTMLParser 使用详解
htmlparser是一个纯的java写的html解析的库,它不依赖于其它的java库文件,主要用于改造或 提取html.它能超高速解析html,而且不会出错.现在htmlparser最新版本为2 ...
- wpa_supplicant 和 802.11g WPA 认证的配置
# cd /etc/init.d# ln -s net.lo net.eth0 默认的接口名是 wlan0,让它开机时自动 up:cp /etc/init.d/net.lo /etc/init.d/n ...
- ORA-00214: control file 控制文件版本不一致
故障现象:今日学习oracle控制文件移动和修改,发现本机安装oracle数据库启动时只使用了一个控制文件.如下:SQL> select * from V$controlfile; STATUS ...
- Flash Builder4.6破解方案(亲测有效)(转)
转自 http://bbs.9ria.com/thread-139463-1-1.html 当修改Host文件无法破解时,需要修改Flash Builder安装目录下某些文件来达到破解的目的,经网上搜 ...
- Floyd-Warshall算法的理解
Floyd算法可以求图内任意两点之间的最短路径,三重循环搞定,虽然暴力,但是属于算法当中最难的动态规划的一种,很有必要理解. 花了一晚上和半个下午专门看这个,才看个一知半解,智商被碾压没办法. 我一直 ...