题目描述

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

建树方法采用“先序遍历+空树用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. ORA-00600: internal error code, arguments: [kcblasm_1], [103], [] bug

    巡检发现存在alert 日志存在ORA-600 1.0 查询alter 对应的Trace日志 /oracle/admin/fgsquery/udump/fgsquery_ora_21777.trc O ...

  2. The NMEA 0183 Protocol

    The NMEA 0183 Protocol NMEA0183 协议是由美国国家海洋电子协会开发.维护并发布的标准,用于航海远洋时使用的电子仪器之间的通信. 目前大部分的 GPS 接受设备都遵循这一标 ...

  3. RMQ_ST表

    ]; ]; ]; void init(int n) { int i, j; pwr[] = ; ; i<; ++i) pwr[i] = pwr[i-] << ; ; pwr[j]&l ...

  4. CodeForces - 441D: Valera and Swaps(置换群)

    A permutation p of length n is a sequence of distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). A permu ...

  5. maven 构建 war文件&&Glassfish运行+部署war文件+访问(命令行模式)

    Glassfish常用命令 asadmin  start-domain  --verbose                 #启动Glassfish服务器(默认domain1) ,并在终端显示相关信 ...

  6. redis和memcache的区别(总结)

    1.Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等: 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供lis ...

  7. German Collegiate Programming Contest 2013-B:Booking(贪心)

        Booking Pierre is in great trouble today! He is responsible for managing the bookings for the AC ...

  8. mysql设置更改root密码、mysql服务器的连接、mysql常用命令

     1.设置更改root密码 查看mysql 启动与否,若没启动就运行:/usr/local/mysql56/bin/mysqlps aux |grep mysql  或 netstat -tulnp ...

  9. Visual Studio 2019 RC

    Visual Studio 2019 RC入门 介绍 在本文中,让我们看看如何开始使用Visual Studio 2019 RC.Microsoft现已发布Visual Studio Release ...

  10. 【二分图最大权完美匹配】【KM算法】【转】

    [文章详解出处]https://www.cnblogs.com/wenruo/p/5264235.html KM算法是用来求二分图最大权完美匹配的.[也就算之前的匈牙利算法求二分最大匹配的变种??] ...