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/ 二叉树的锯齿形层次遍历 给出 ...
随机推荐
- 获取列表中的最大的N项和最小的N项
获取列表中的最大的N项和最小的N项 #!/sur/bin/env python # -*- coding:utf-8 -*- # author:zengsf #time:2018/10/31 impo ...
- [LeetCode&Python] Problem 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- 多行文本用textarea而不是input type=textarea“”
<textarea name="zhaiyao" id="" cols="35" rows="4">< ...
- 数据库(linux)
基本指令整合: 删除数据库 drop table department 修改表中已有记录 update company set salary=3000 where age-25: u ...
- Java异常处理的方法
1.异常概述在程序中,错误可能产生于程序员没有预料到的各种情况,或者是超出了程序员可控制范围的环境因素,如用户的坏数据.试图打开一个根本不存在的文件等.在Java中这种在程序运行是可能出现的一些错误称 ...
- L3-019 代码排版 (30 分)
某编程大赛中设计有一个挑战环节,选手可以查看其他选手的代码,发现错误后,提交一组测试数据将对手挑落马下.为了减小被挑战的几率,有些选手会故意将代码写得很难看懂,比如把所有回车去掉,提交所有内容都在一行 ...
- 03 重定向,请求转发,cookie,session
重定向: /* 之前的写法 response.setStatus(302); response.setHeader("Location", "login_success. ...
- 计算字符串最后一个单词的长度,单词以空格隔开。 java算法
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = ne ...
- LeetCode - Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...