DS二叉树--层次遍历
题目描述
层次遍历二叉树,是从根结点开始遍历,按层次次序“自上而下,从左至右”访问树中的各结点。
建树方法采用“先序遍历+空树用0表示”的方法
要求:采用队列对象实现,函数框架如下:
输入
第一行输入一个整数t,表示有t个测试数据
第二行起输入二叉树先序遍历的结果,空树用字符‘0’表示,输入t行
输出
逐行输出每个二叉树的层次遍历结果
样例输入
样例输出
#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二叉树--层次遍历的更多相关文章
- [Leetcode] Binary tree level order traversal二叉树层次遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 毕业了C++二叉树层次遍历
//代码经过测试,赋值粘贴即可用#include<iostream> #include<stdio.h> #include<stack> #include<q ...
- 毕业了-java二叉树层次遍历算法
/*************************************** * 时间:2017年6月23日 * author:lcy * 内容:二叉树的层次遍历 * 需要借助队列这个数据结构,直 ...
- 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- 剑指 Offer 32 - I. 从上到下打印二叉树 + 层次遍历二叉树
剑指 Offer 32 - I. 从上到下打印二叉树 Offer_32_1 题目描述 解题思路 这题属于简单题,考察的是我们对二叉树以及层次遍历的方法. 这里只需要使用简单的队列即可完成二叉树的层次遍 ...
- [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 ...
- [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 算法与数据结构(三) 二叉树的遍历及其线索化(Swift版)
前面两篇博客介绍了线性表的顺序存储与链式存储以及对应的操作,并且还聊了栈与队列的相关内容.本篇博客我们就继续聊数据结构的相关东西,并且所涉及的相关Demo依然使用面向对象语言Swift来表示.本篇博客 ...
- lintcode二叉树的锯齿形层次遍历 (双端队列)
题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...
随机推荐
- Echarts 绘图(饼图,中国地图)
一个使用Javascript 实现的开源可视化库,可以流畅的运行在pc 和移动设备上,兼容当前绝大部分浏览器(Chrome ,firefox,IE8)等 底层依赖轻量级的矢量图形库ZRender,提供 ...
- 对Functional Language的认识
What: A functional language is a programming language built over and around logical functions or pro ...
- python函数完整语法和分类
函数初级 简介 # 函数是一系列代码的集合,用来完成某项特定的功能 优点 '''1. 避免代码的冗余2. 让程序代码结构更加清晰3. 让代码具有复用性,便于维护''' 函数四部分 '''1. 函数名: ...
- 洛谷P2051 中国象棋(dp)
题目链接:传送门 题目大意: 在N行M列的棋盘中放象棋中的“炮”,问要使得“炮”两两互不伤害,有多少种放法. 1 ≤ n,m ≤ 100,答案对9999973取模. 思路: 按行更新答案.每行炮可以放 ...
- linux cron计划任务、chkconfig 命令、systemd命令、unit 相关、target 相关
1.设置说明位置 : cat /etc/crontab # Example of job definition:# .---------------- minute (0 - 59)# | .---- ...
- n!的质因子分解
其中k为任意质因子,因为a的数值不确定,所有k的值可以任意选择. 以下代码用于求出m!: #include<bits/stdc++.h> LL getpow(LL n,LL k) { LL ...
- python scrapy 爬虫实例
1 创建一个项目 scrapy startproject basicbudejie 2 编写爬虫 import scrapy class Basicbudejie(scrapy.Spider): na ...
- HDU 4642 Fliping game (简单博弈)
Fliping game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 【HDOJ3567】【预处理bfs+映射+康拓展开hash】
http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others) Me ...
- putty登陆sourceforge.net(设置登录)
打开putty.exe session选项的host name (ip address) 填写 shell.soureceforge.net 端口22(不变) 接下来是connection选项子目录下 ...