CSharp Algorithm - How to traverse binary tree by breadth (Part II)
来源:互联网 发布:国标网络协议 编辑:程序博客网 时间:2024/06/02 01:28
/*
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)
- CSharp Algorithm - How to traverse binary tree by depth (Part I)
- Traverse binary tree by level
- how to manually traverse the ZODB tree in your code to locate objects by their path?
- How to traverse a directory tree
- Breadth-first Search -- Leetcode problem107. Binary Tree Level Order Traversal II
- How to Redial ADSL Dial-up Connection by CSharp
- Breadth-first traverse
- How to Draw a Binary Tree ?
- How to Draw a Balanced Binary Tree ?
- [Leetcode] Postorder Traverse of Binary Tree
- leetcode之Binary Tree Level Order Traverse
- How to traverse Hashtable in Java
- How-to create a Calculator Using Google Android: Part II
- Knowledge Conclusion - PART II : CSharp, .NET
- Breadth-first Search -- Leetcode problem102. Binary Tree Level Order Traversal
- Tree Traverse
- How To Build a Balance Binary Tree(BBT)
- VMware vSphere学习之架构
- lr录制文件下载的脚本
- 各种各样的jdbc数据库连接方式【目前关注Altibase和timesten】
- 【面试题】如何让C语言自动发现泄漏的内存
- jsp获取当前路径
- CSharp Algorithm - How to traverse binary tree by breadth (Part II)
- C语言如何产生随机数
- HDU Moving Tables
- 在Qt creator中使用Qt for VS2008建立Debug工具
- [HDU 3308]LCIS[线段树][区间合并]
- MainActivity 会异步加载图片到相应的ImageView上
- 汉字的存放顺序和传输顺序是不一样的
- 如何修改WiFi Direct default名称中"Andorid"字样
- 在Timer和ScheduledExecutorService间决择