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 ...
随机推荐
- 《ArcGIS Engine+C#实例开发教程》第八讲 属性数据表的查询显示
原文:<ArcGIS Engine+C#实例开发教程>第八讲 属性数据表的查询显示 第一讲 桌面GIS应用程序框架的建立 第二讲 菜单的添加及其实现 第三讲 MapControl与Page ...
- 知识总结: Activity的四种启动模式
通常情况下,一个应用有一个Task,这个Task就是为了完成某个工作的一系列Activity的集合.而这些Activity又被组织成了堆栈的形式.当一个Activity启动时,就会把它压入该Task的 ...
- spring cloud官方文档提到的服务开发的12项要素。
I. Codebase 从一个代码库部署到多个环境. II. Dependencies 使用显式的声明隔离依赖,即模块单独运行,并可以显式管理依赖. III. Config 在系统外部存储配置信息. ...
- 关于fastclick.js
Fastclick fastclick.js解决了什么问题? 自己接触WebApp开发的前期, 总感觉WebApp上的按键操作不如NativeApp的灵敏, 好像有那么一小点延迟. 后来才知道, 这是 ...
- 如何查看自己运行ubuntu是32位还是64位
当安装ubuntu在pc上,不推荐在32位pc安装64位操作系统,64位pc安装32位操作系统 方法/步骤 按ctrl+shift+t 快捷键,打开终端,输入sudo uname --m ,按下ent ...
- ActionBar官方教程(10)ActionBar的下拉列表模式
Adding Drop-down Navigation As another mode of navigation (or filtering) for your activity, the acti ...
- BrandZ:2016年全球最具价值品牌百强榜(完整报告)
https://wppbaz.com/admin/uploads/files/BZ_Global_2016_Report.pdf Millward Brown编制的BrandZ最新排行榜(2016 B ...
- 如何在GeoServer上发布一张地图
在GeoServer上发布一张地图步骤大致如下: 先准备一张地图,格式可以是:jpg.png.tif等. Jpg文件对应的坐标信息文件为jgw格式文件,投影文件为prj文件;Tif文件对应的坐标信息文 ...
- Git branch (分支学习)
以前总结的一些git操作,分享在这里. Git 保存的不是文件差异或者变化量,而只是一系列文件快照. - 列出当前所有分支 git branch <--merge> | <--n ...
- 对 Linux 专家非常有用的 20 个命令
原文链接:http://www.oschina.net/translate/20-advanced-commands-for-linux-experts?from=20130811 对中级 Linux ...