题目描述

层次遍历二叉树,是从根结点开始遍历,按层次次序“自上而下,从左至右”访问树中的各结点。

建树方法采用“先序遍历+空树用0表示”的方法

要求:采用队列对象实现,函数框架如下:

输入

第一行输入一个整数t,表示有t个测试数据

第二行起输入二叉树先序遍历的结果,空树用字符‘0’表示,输入t行

输出

逐行输出每个二叉树的层次遍历结果

样例输入

2
AB0C00D00
ABCD00E000FG00H0I00

样例输出

ABDC
ABFCGHDEI
#include<iostream>
#include<string>
#include<queue>
using namespace std;
class BitreeNode
{
public:
char data;
BitreeNode *LeftChild, *RightChild;
BitreeNode() :LeftChild(NULL), RightChild(NULL) {}
~BitreeNode() {}
};
class Bitree
{
private:
BitreeNode *Root;
int pos, sum;
string strTree;
void LevelOrder(BitreeNode *t)
{
queue<BitreeNode*> tq;
BitreeNode *p = t;
int x = ;
while (x < sum)
{
if (p)
tq.push(p);
while (!tq.empty())
{
p = tq.front();
tq.pop();
cout << p->data;
x++;
if (p->LeftChild)
tq.push(p->LeftChild);
if (p->RightChild)
tq.push(p->RightChild);
}
}
}
BitreeNode *CreateBitree()
{
BitreeNode *T;
char ch;
ch = strTree[pos++];
if (ch == '')
T = NULL;
else
{
sum++;
T = new BitreeNode();
T->data = ch;
T->LeftChild = CreateBitree();
T->RightChild = CreateBitree();
}
return T;
}
public:
Bitree() {}
~Bitree(){}
void CreateBitree(string TreeArray)
{
pos = ;
sum = ;
strTree.assign(TreeArray);
Root = CreateBitree();
}
void LevelOrder()
{
LevelOrder(Root);
}
}; int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
Bitree *Tree;
Tree = new Bitree();
Tree->CreateBitree(str);
Tree->LevelOrder();
cout << endl;
}
return ;
}

DS二叉树--层次遍历的更多相关文章

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

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

  2. 毕业了C++二叉树层次遍历

    //代码经过测试,赋值粘贴即可用#include<iostream> #include<stdio.h> #include<stack> #include<q ...

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

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

  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. 百练6255-单词反转-2016正式B题

    百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问   B:单词翻转 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个 ...

  2. EmBitz1.11中将左边的目录弄出来

    在view→manager   然后就会出来

  3. [LeetCode&Python] Problem 429. N-ary Tree Level Order Traversal

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  4. 软件产品案例分析——福州大学微信小程序

    一 .调研,评测 评测 第一次上手体验: 刚进入看到菜单界面,感觉还是比较生动清晰的,功能很多,也很全面,包涵了大部分学生所需要的功能,就是第一次身份验证那里找了半天. bug: 1.点击进入学生证附 ...

  5. Autofac解耦事件总线

    事件总线之Autofac解耦 事件总线是通过一个中间服务,剥离了常规事件的发布与订阅(消费)强依赖关系的一种技术实现.事件总线的基础知识可参考圣杰的博客[事件总线知多少] 本片博客不再详细概述事件总线 ...

  6. java中事件驱动

    在java语言中,事件不是由事件源自己来处理的,而是交给事件监听者来处理,要将事件源(如按钮)和对事件的具体处理分离开来.这就是所谓的事件委托处理模型. 事件委托处理模型由产生事件的事件源.封装事件相 ...

  7. django实现api跨域请求访问

    第一步:安装 django-cors-headers pip install django-cors-headers 第二步:配置settings.py文件 --------------------- ...

  8. Go Example--通道遍历

    package main import ( "fmt" ) func main() { queue := make(chan string, 2) queue <- &quo ...

  9. s3c2410串口笔记

  10. Terraform Detecting Drift

    转自:https://www.terraform.io/docs/extend/best-practices/detecting-drift.html 这篇文章主要说明了对于资源如何处理 read&a ...