void BFSLayer(BinaryTreeNode* pRoot)
{
if(pRoot==nullptr)
return;
queue<BinaryTreeNode*> pNode;
int curLayer=,nextLayer=;
pNode.push(pRoot);
while(!pNode.empty())
{
BinaryTreeNode* pFront=pNode.front();
cout<<pFront->m_Value<<' ';
pNode.pop();
curLayer--;
if(pFront->m_pLeft!=nullptr)
{
nextLayer++;
pNode.push(pFront->m_pLeft);
}
if(pFront->m_pRight!=nullptr)
{
nextLayer++;
pNode.push(pFront->m_pRight);
}
if(curLayer==)
{
curLayer=nextLayer;
nextLayer=;
cout<<endl;
}
}
}

函数

 #include"BinaryTree.h"

 // ====================测试代码====================
// 8
// 6 10
// 5 7 9 11
void Test1()
{
BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode10 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode9 = CreateBinaryTreeNode();
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode8, pNode6, pNode10);
ConnectTreeNodes(pNode6, pNode5, pNode7);
ConnectTreeNodes(pNode10, pNode9, pNode11); printf("====Test1 Begins: ====\n");
printf("Expected Result is:\n");
printf("8 \n");
printf("6 10 \n");
printf("5 7 9 11 \n\n"); printf("Actual Result is: \n");
BFSLayer(pNode8);
printf("\n"); DestroyTree(pNode8);
} // 5
// 4
// 3
//
void Test2()
{
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode5, pNode4, nullptr);
ConnectTreeNodes(pNode4, pNode3, nullptr);
ConnectTreeNodes(pNode3, pNode2, nullptr); printf("====Test2 Begins: ====\n");
printf("Expected Result is:\n");
printf("5 \n");
printf("4 \n");
printf("3 \n");
printf("2 \n\n"); printf("Actual Result is: \n");
BFSLayer(pNode5);
printf("\n"); DestroyTree(pNode5);
} // 5
// 4
// 3
// 2
void Test3()
{
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode5, nullptr, pNode4);
ConnectTreeNodes(pNode4, nullptr, pNode3);
ConnectTreeNodes(pNode3, nullptr, pNode2); printf("====Test3 Begins: ====\n");
printf("Expected Result is:\n");
printf("5 \n");
printf("4 \n");
printf("3 \n");
printf("2 \n\n"); printf("Actual Result is: \n");
BFSLayer(pNode5);
printf("\n"); DestroyTree(pNode5);
} void Test4()
{
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(); printf("====Test4 Begins: ====\n");
printf("Expected Result is:\n");
printf("5 \n\n"); printf("Actual Result is: \n");
BFSLayer(pNode5);
printf("\n"); DestroyTree(pNode5);
} void Test5()
{
printf("====Test5 Begins: ====\n");
printf("Expected Result is:\n"); printf("Actual Result is: \n");
BFSLayer(nullptr);
printf("\n");
} // 100
// /
// 50
// \
//
void Test6()
{
BinaryTreeNode* pNode100 = CreateBinaryTreeNode();
BinaryTreeNode* pNode50 = CreateBinaryTreeNode();
BinaryTreeNode* pNode150 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode100, pNode50, nullptr);
ConnectTreeNodes(pNode50, nullptr, pNode150); printf("====Test6 Begins: ====\n");
printf("Expected Result is:\n");
printf("100 \n");
printf("50 \n");
printf("150 \n\n"); printf("Actual Result is: \n");
BFSLayer(pNode100);
printf("\n");
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6(); return ;
}

测试代码

剑指offer——面试题32.1:分行从上到下打印二叉树的更多相关文章

  1. 《剑指offer》— JavaScript(22)从上往下打印二叉树

    从上往下打印二叉树 题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路 借助两个辅助队列,一个用来存放结点,一个用来存放结点值: 先将根节点加入到队列中,然后遍历队列中的元素,遍历 ...

  2. 《剑指offer》第三十二题(分行从上到下打印二叉树)

    // 面试题32(二):分行从上到下打印二叉树 // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层 // 打印到一行. #include <cstdio> #in ...

  3. 《剑指offer》第三十二题(不分行从上往下打印二叉树)

    // 面试题32(一):不分行从上往下打印二叉树 // 题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印. #include <iostream> #include ...

  4. 《剑指offer》第三十二题(之字形打印二叉树)

    // 面试题32(三):之字形打印二叉树 // 题目:请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺 // 序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印, / ...

  5. 【剑指offer】不分行从上到下打印二叉树,C++实现(层序遍历)

    原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目 从上往下打印出二叉树的每个节点,同层节点从左至右打印.例如: 图  不分行从上往下按层打印二叉 ...

  6. 剑指Offer:面试题32——从1到n整数中1出现的次数(java实现)

    问题描述: 输入一个整数n,求1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11,12,1一共出现了5次. 思路:(不考虑时间效率的解法,肯定不 ...

  7. 剑指offer——面试题32:从上到下打印二叉树

    void BFS(BinaryTreeNode* pRoot) { if(pRoot==nullptr) { cout<<"empty binary tree!"< ...

  8. 剑指offer 分行从上到下打印二叉树

    题目: 从上到下按层打印二叉树,同一层的节点按照从左到右的顺序打印,每一层打印到一行. /* struct TreeNode { int val; struct TreeNode *left; str ...

  9. 剑指offer——33分行从上到下打印二叉树

    题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行.   题解: 使用BFS,按层打印即可 class Solution { public: vector<vector&l ...

随机推荐

  1. 关于InvokeMethod Activity的异步调用

    讨论地址:http://www.cnblogs.com/foundation/archive/2009/12/17/1626617.html 结论是IsCompleted的设置被忽略,看代码里注释 u ...

  2. Apache配置伪静态

    Apache配置伪静态 注意:本文中关于Apache的配置修改,一定要记得重启Apache服务 伪静态的实现有多种方法,比如通过获取path_info信息使用php逻辑来达到伪静态,使用Apache提 ...

  3. java成长之路-开篇

    一,为了生活 从业7年,主要还是运用.net过日子.今儿下决心再次准备学习java并想达到一定高度,也还是想以后能主要用java赚钱过日子.抱歉眼界所到,平均情况下,java平台的收入还是比.net的 ...

  4. LNMP详细介绍

    1>Nginx概述: 很多人对apache非常熟悉,Nginx与Apache类似,属于WEB容器,同时也是一款高性能的HTTP和反向代理软件,它们之间最大的差别是Apache的处理速       ...

  5. [记]Centos下流量统计使用记录

    因为最近要进行centos流量统计,需求是想针对tomcat进行针对性的上下行流量时段统计及汇总,找了很多资料及命令,要么是可以针对进程的但是没有汇总,要么是有汇总但是不针对进程. 所以只能混合几个命 ...

  6. c++define的用法

    c++define的用法   在写程序时经常会碰到这样一个问题,我们需要重复写很多相同的代码,并且这些代码结构相同.总是想自己把这段代码封装一下然后直接进行调用,但是如果这段代码逻辑并不复杂,并且代码 ...

  7. char、varchar、nchar、nvarchar、text的区别

    char.varchar.nchar.nvarchar.text的区别 1.有var前缀的,表示是实际存储空间是变长的,varchar,nvarchar 所谓定长就是长度固定的,当输入的数据长度没有达 ...

  8. JSON is undefined. Infopath Form People Picker in SharePoint 2013

    After some analysis, we found that, this is a known defect with the Microsoft and it is being fixed ...

  9. sharepoint 通过数据库擅长列表项

    select *from [dbo].[AllLists] where tp_Title='Pages' and tp_WebId='68BDFC9A-4E0C-425E-9985-573CD6716 ...

  10. php如何进行多进程与异步调用方法

    浏览器和服务器之间只一种面向无连接的HTTP协议进行通讯的,面向无连接的程序的特点是客户端请求服务端,服务端根据请求输出相应的程序,不能保持持久连接. 这样就出现了一个问题,一个客户端的相应服务端可能 ...