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 ...
随机推荐
- Android 获得屏幕的宽高度
在View构造函数中获得屏幕的宽高 public class GameView extends View { public GameView(Context context) { Display d ...
- yiic 数据库迁移工具
数据库的结构也同源代码一样随着我们开发的进行而不断的发生着改变.在开发过程中,一般的我们需要像管理我们的源代码一样记录下数据库结构的整个变化过程,以便代码还原到指定版本后,数据库能同步的还原到指定的版 ...
- ruby mysql数据库操作
require 'mysql' con=Mysql.new('localhost','root','root','test') con.query('set names utf8') rs=con.q ...
- 【HDOJ】1197 Specialized Four-Digit Numbers
水题,暴力可解. #include <iostream> using namespace std; int chg(int n, int base); int main() { int i ...
- Android开发UI之补间动画-Tween Animation
Tween Animation-补间动画 官网链接-http://developer.android.com/reference/android/view/animation/Animation.ht ...
- cnb
<style type="text/css"> .fieldset_s{border: 1px #dedede solid;padding: 19px; color: ...
- MySQL海量数据查询优化策略
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- WCF 配置服务 (02)
配置服务概述 • 在设计和实现服务协定后,即可配置服务. 在其中可以定义和自定义如何向客户端公开服务,包括指定可以找到服务的地址.服务用于发送和接收消息的传输和消息编码,以及服务需要的安全类型. • ...
- LittleTool之批量修改材质
using UnityEngine; using System.Collections; using UnityEditor; public class ChangeMaterial : Editor ...
- linux的nohup命令的用法
在应用Unix/Linux时,我们一般想让某个程序在后台运行,于是我们将常会用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/mysql/bin/my ...