二叉树的遍历分为BFS和DFS两种大类
下面完整实现BFS遍历二叉树

 * 例如二叉树*      1*     / \*    2   3*   /\* 4  5

BFS遍历结果:1-2-3-4-5

具体的代码实现:

方法一、采用递归遍历的方法实现

// Recursive C program for level order traversal of Binary Tree
#include <stdio.h>
#include <stdlib.h>/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{int data;struct node* left, *right;
};/* Function protoypes */
void printGivenLevel(struct node* root, int level);
int height(struct node* node);
struct node* newNode(int data);/* Function to print level order traversal a tree* 方法一:* 方法二见05* */
void printLevelOrder(struct node* root)
{int h = height(root);int i;for (i=1; i<=h; i++)printGivenLevel(root, i);
}/* Print nodes at a given level */
void printGivenLevel(struct node* root, int level)
{if (root == NULL)return;if (level == 1)printf("%d ", root->data);else if (level > 1){//递归调用printGivenLevel(root->left, level-1);printGivenLevel(root->right, level-1);}
}/* Compute the "height" of a tree -- the number ofnodes along the longest path from the root nodedown to the farthest leaf node.*/
int height(struct node* node)
{if (node==NULL)return 0;else{/* compute the height of each subtree */int lheight = height(node->left);int rheight = height(node->right);/* use the larger one */if (lheight > rheight)return(lheight+1);else return(rheight+1);}
}/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{struct node* node = (struct node*)malloc(sizeof(struct node));node->data = data;node->left = NULL;node->right = NULL;return(node);
}/* Driver program to test above functions*/
int main()
{struct node *root = newNode(1);root->left   = newNode(2);root->right    = newNode(3);root->left->left = newNode(4);root->left->right = newNode(5);printf("Level Order traversal of binary tree is \n");printLevelOrder(root);return 0;
}

输出:

Level Order traversal of binary tree is
1 2 3 4 5

方法二、采用先进先出的队列实现

#include <stdio.h>
#include <stdlib.h>
#define MAX_Q_SIZE 500
/*构造树的结点* 一个树有一个数据域,一个左孩子指针,一个右孩子指针*/
struct node {int data;struct node *left;struct node *right;
};/*几个方法原型*/
struct node **createQueue(int *, int *);//创建一个队列,用双指针
void enQueue(struct node **, int *, struct node *);//第一个参数是双指针
struct node *deQueue(struct node **, int *);//出队/*创建一个队列,这个队列是以struct node属性的数组存储的* 此队列,相当于一个数据类型是struct node的数组*/
struct node **createQueue(int *front, int *rear) {//为队列申请内存,这里用双指针struct node **queue = (struct node **) malloc(sizeof(struct node *)*MAX_Q_SIZE);//初始化的时候,一定记得*front = *rear处于同一位置*front = *rear = 0;return queue;
}/*入队,尾入*/
void enQueue(struct node **queue, int *rear, struct node *new_node) {queue[*rear] = new_node;//现将结点放到*rear的位置(*rear)++;//加一
}/*出队,头出*/
struct node *deQueue(struct node **queue, int *front)
{(*front)++;return queue[*front - 1];
}/*通过给定的数据域,直接构造一个树的结点*/
struct node * newNode(int data){//要构造新节点,首先要想到的就是为结点申请内存struct node *node = (struct node *)malloc(sizeof(struct node));node->data = data;node->left = NULL;node->right = NULL;return node;
}/*给一个二叉树,要层次打印出各个结点,通过队列的方式实现*/
void printLevelOrder(struct node* root){//定义变量int front,rear;//创建队列,传地址&struct node **queue = createQueue(&front,&rear);//创建一个临时的树结点,将root指针指向的结点地址赋值指针temp_node//因为root已经是指针类型,所以可以直接赋值struct node *temp_node = root;//遍历。指针temp_node处的结点存在while (temp_node){//打印printf("%d ",temp_node->data);//如果左孩子存在,左孩子入队if (temp_node->left){enQueue(queue,&rear,temp_node->left);}//如果右孩子存在,右孩子入队if (temp_node->right){enQueue(queue,&rear,temp_node->right);}//出队一个结点指针,使他赋值给temp_node指针temp_node = deQueue(queue,&front);}
}/*** BFS 广度优先遍历* 对于每个树的结点来说:* 第一次访问这个结点后,就将这个结点的的孩子结点放入先进先出的队列中* @return*/
int main() {//初始化一个根结点struct node *root = newNode(1);root->left = newNode(2);root->right = newNode(3);root->left->left = newNode(4);root->left->right = newNode(5);printf("Level Order traversal of binary tree is \n");printLevelOrder(root);return 0;
}

输出:

Level Order traversal of binary tree is
1 2 3 4 5

以上两种方式,第二种相对来说比较容易理解一些。

01 二叉树的BFS(广度、层次或水平遍历实现)【Binary Tree 二叉树】相关推荐

  1. [LeetCode] Invert Binary Tree - 二叉树翻转系列问题

    目录: 1.Invert Binary Tree - 二叉树翻转 [递归] 题目概述: Invert a binary tree. 4/ \2 7/ \ / \ 1 3 6 9 to 4/ \7 2/ ...

  2. LeetCode 545. Boundary of Binary Tree 二叉树边界

    LeetCode 545. Boundary of Binary Tree 二叉树边界 Given a binary tree, return the values of its boundary i ...

  3. 15 二叉树的中序遍历(Binary Tree Inorder Traversal)

    文章目录 1 题目 2 描述 3 解决方案 3.1 递归算法 3.1.1 遍历法(Traverse) 思路 源码 3.1.2 分治法(Devide And Conquer) 思路 源码 3.2 非递归 ...

  4. 14 二叉树的前序遍历(Binary Tree Preorder Traversal)

    文章目录 1 题目 2 描述 3 解决方案 3.1 递归算法 3.1.1 遍历法(Traverse) 思路 源码 3.1.2 分治法(Devide And Conquer) 思路 源码 3.2 非递归 ...

  5. 二叉树遍历(Binary Tree Traversal)

    二叉树遍历(Binary Tree Traversal) 二叉树的递归遍历比较简单,这里说一下非递归遍历,以中序遍历为例子. 非递归遍历主要用到栈来协助进行.对于一个二叉树,首先根节点入栈,如果有左儿 ...

  6. C语言实现二叉树的中序线索化及遍历中序线索二叉树

    C语言实现二叉树的线索化以及如何遍历线索二叉树! 文章目录 线索二叉树的结构及数据类型定义 根据输入结点初始化二叉树 中序遍历二叉树并线索化 遍历中序线索二叉树 项目完整代码 项目完整代码(改进版) ...

  7. leetcode 二叉树的层次遍历 II(Binary Tree Level Order Traversal II)

    目录 题目描述: 示例: 解法: 题目描述: 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 示例: 给定二叉树 [3,9,20,null ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. LeetCode:104_Maximum Depth of Binary Tree | 二叉树的最大深度 | Easy

    要求:求二叉树的深度(二叉树的深度为最远叶子节点到根节点的距离,即根节点到最远叶子节点的距离) Given a binary tree, find its maximum depth.The maxi ...

最新文章

  1. int数组转化为字符数组 java_Java 将int数组转换为字符串
  2. 3000+ NLP资源一网打尽,只需用这个分类检索网站 | 免费
  3. HDU1081 (最大连续子序列/矩阵)
  4. 纠结的STM32 RTC时钟源LSE
  5. 【2019暑假刷题笔记-STL绪论】总结自《算法笔记》
  6. PMCAFF | 刷微信朋友圈行为分析:刷朋友圈是一种感觉?有一种批皇帝批奏章的感觉...
  7. java ehcahce刷新_springboot结合ehcache防止恶意刷新请求的实现
  8. Js中 关于top、clientTop、scrollTop、offsetTop的用法
  9. PHP笔记-平润年判断例子
  10. python新建以时间命名的目录
  11. laravel引入自定义全局函数
  12. 性能测试之性能监控技术
  13. 三年制专转本计算机,三年制专转本和五年制专转本的区别
  14. Mysql SQLyog 使用详解
  15. 对抗极域电子教室#破解、解除
  16. IDEA2021.03 Tomcat热部署的实现
  17. redis 40道面试题
  18. 加域时提示指定的网络名不再可用
  19. 对梯度概念的直观理解
  20. 隔离出来的“陋室铭”

热门文章

  1. 地理坐标系和投影坐标系的联系_收藏| 地图投影系列介绍(二)——地理坐标系...
  2. 多域名SSL证书介绍
  3. Java—学生管理系统使用文件永久存储
  4. 华钜同创:都2022年了,你还在用那些老掉牙的选品方法???
  5. 用C语言写学生成绩管理系统
  6. 中国大学生数学竞赛(非数学专业类)竞赛大纲
  7. android删除所有已保存wifi密码,安卓移除/忘记已保存的wifi密码
  8. 有容云:容器驱动的PaaS平台实现方案(上)
  9. summernote图片上传
  10. 国标GB28181介绍