//代码经过测试,赋值粘贴即可用
#include<iostream>
#include<stdio.h>
#include<stack>
#include<queue>
#include<malloc.h>
using namespace std; //二叉树结点
typedef struct BTNode{
char data;
struct BTNode *lchild;
struct BTNode *rchild;
}BTNode; //模板先序建立二叉树,大话p187,教科书吧版
BTNode *CreateBiTree()//只需要一个函数
{
char ch;
BTNode *T;
scanf("%c",&ch);
if(ch=='#')
T=NULL;
else
{
T = (BTNode *)malloc(sizeof(BTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;//返回根节点
} //层次遍历
void LevelOrder(BTNode *T){
//queue<BTNode> queue;大bug隐藏在这个地方;注意queue这个容器装的是什么东西
queue<BTNode *> queue;
queue.push(T); //算法:根结点入队列 while(!queue.empty()){ //若队列非空则循环执行下列的3个步骤 T = queue.front(); //步骤1:对头元素出队,指针从新指向,front()方法是将返回队头元素
printf("%c ",T->data);//队头元素出队然后将队头元素的左右孩子入队
queue.pop();//pop是出队 if(T->lchild != NULL){//步骤2:左子树不空,将左子树入队
queue.push(T->lchild);//入队的就是一个地址元素
} if(T->rchild != NULL){//步骤3:右子树不空,将右子树入队
queue.push(T->rchild);
}
}
}
int main()
{
BTNode *T;
T = CreateBiTree();//建立 LevelOrder(T); return ;
}

毕业了C++二叉树层次遍历的更多相关文章

  1. 毕业了-java二叉树层次遍历算法

    /*************************************** * 时间:2017年6月23日 * author:lcy * 内容:二叉树的层次遍历 * 需要借助队列这个数据结构,直 ...

  2. [Leetcode] Binary tree level order traversal二叉树层次遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  3. DS二叉树--层次遍历

    题目描述 层次遍历二叉树,是从根结点开始遍历,按层次次序“自上而下,从左至右”访问树中的各结点. 建树方法采用“先序遍历+空树用0表示”的方法 要求:采用队列对象实现,函数框架如下: 输入 第一行输入 ...

  4. 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树

    题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...

  5. 剑指 Offer 32 - I. 从上到下打印二叉树 + 层次遍历二叉树

    剑指 Offer 32 - I. 从上到下打印二叉树 Offer_32_1 题目描述 解题思路 这题属于简单题,考察的是我们对二叉树以及层次遍历的方法. 这里只需要使用简单的队列即可完成二叉树的层次遍 ...

  6. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  8. 算法与数据结构(三) 二叉树的遍历及其线索化(Swift版)

    前面两篇博客介绍了线性表的顺序存储与链式存储以及对应的操作,并且还聊了栈与队列的相关内容.本篇博客我们就继续聊数据结构的相关东西,并且所涉及的相关Demo依然使用面向对象语言Swift来表示.本篇博客 ...

  9. lintcode二叉树的锯齿形层次遍历 (双端队列)

    题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...

随机推荐

  1. Bootstrap风格zTree树形菜单插件

    这是一款bootstrap风格jQuery zTree树形菜单插件,支持自定义编辑.添加列表菜单.删除列表等功能的jQuery树形菜单代码.在线演示 具体代码实现: <!DOCTYPE html ...

  2. springcloud11----turbine

    package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframew ...

  3. #ZLYD团队第二周项目总结

    ZLYD团队第二周项目总结 项目进展 确定项目内容.目标.实现计划 首先确定游戏界面的游戏区域中墙的位置,绘制其图形,并通过其中的方法,返回墙壁的位置等属性. 根据豆子的位置,绘制其图形. 初始化吃豆 ...

  4. usb_control_msg() -- 从设备读取各种信息

    et_port_status() --> usb_control_msg()usb_get_descriptor() --> usb_control_msg()/usr/src/linux ...

  5. Win32程序支持命令行参数的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 首先说说Win 32 API程序如何支持命令行参数.Win 32程序的入口函数为: int APIENTRY _tWi ...

  6. 更换Ubuntu14.04主题

    闲暇之余,想玩一玩Ubuntu的主题,想把原来的主题换成Numix主题,说干就干. sudo add-apt-repository ppa:numix/ppa sudo apt-get update ...

  7. UVa 1637 纸牌游戏(全概率公式)

    https://vjudge.net/problem/UVA-1637 题意: 36张牌分成9堆,每堆4张牌.每次可以拿走某两堆顶部的牌,但需要点数相同.每种拿法的概率均为1/5.求成功概率. 思路: ...

  8. TCGA系列--TCGA可视化数据库GEPIA

    中国大牛力作  张泽民: http://gepia.cancer-pku.cn/index.html http://cancer-pku.cn/

  9. Java中含有静态成员的的初始化顺序

    class Bowl{ Bowl(int marker){ System.out.println("Bowl(" + marker + ")" ); } voi ...

  10. /etc/apt/sources.list.d/ros-latest.list' permission denied

    换为英文的' sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu trusty main" > /etc/apt ...