/*

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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  5. 【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 ...

  6. 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 ...

  7. 63. Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...

  8. [Algorithm] Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  9. [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 ...

随机推荐

  1. RichEdit 各个版本介绍

    RichEdit是开发中经常使用到的控件,其版本自1.0起,历经好几年,好几次的更新,在此引用一篇介绍RichEdit版本的博文(http://blogs.msdn.com/b/murrays/arc ...

  2. leetcode面试准备:Add and Search Word - Data structure design

    leetcode面试准备:Add and Search Word - Data structure design 1 题目 Design a data structure that supports ...

  3. 【HDOJ】1018 Big Number

    数学题,还是使用log避免大数,但是不要忘记需要+1,因为0也是1位,log(100)= 2,但却是3位. #include <stdio.h> #include <math.h&g ...

  4. ununtu卸载软件

    sudo apt-get remove vim

  5. UpdatePanel无法直接弹出窗口的解决

    UpdatePanel无法直接弹出窗口的解决 2010-06-20  来自:博客园  字体大小:[大 中 小] 摘要:本文介绍一些UpdatePanel无法直接弹出窗口的解决方法 ///<sum ...

  6. easyui plugin —— etreegrid:CRUD Treegrid

    昨天写了一个ko+easyui的同样的实现,感觉写的太乱,用起来十分麻烦,于是今天照着edatagrid,写了一个etreegrid,这样再用ko绑定就方便多了. 使用很简单,$(tableId).e ...

  7. sql - and - or

    sql - and SQL AND links together two or more conditional statements for increased filtering when run ...

  8. 编写一个jsp页面,输出九九乘法表。

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  9. [codevs4247]奇特的生物

    题目描述 Description 科学家们最近发现了一种奇怪的生物,它们每天长大一岁,刚出生的宝宝为1岁,且它们的年龄没有上限.已知年龄为1岁,2岁,3岁,……,k岁的个体具有生育能力,当年龄为i的具 ...

  10. 解决Mac下Sequel Pro 1.1 连接 Homebrew安装Mysql5.7.8的问题 Sequel Pro 1.1 encountered an unexpected error

    解决Mac下Sequel Pro 1.1 连接 Homebrew安装Mysql5.7.8的问题 Sequel Pro encountered an unexpected error Sequel Pr ...