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. asp.net 4高级程序设计( 第4版)文摘

    第一部分 核心概念 第1章 asp.net 简介 第2章 visual studio 第3章 Web窗体 3.2 web窗体处理阶段 页面框架初始化(page.init),用户代码初始化(page.l ...

  2. 【转】Defunct进程 僵尸进程

    在测试基于 DirectFB+Gstreamer 的视频联播系统的一个 Demo 的时候,其中大量使用 system 调用的语句,例如在 menu 代码中的 system("./play&q ...

  3. Gym 101201J Shopping (线段树+取模)

    题意:给定 n 个物品,然后有 m 个人买东西,他们有 x 元钱,然后从 l - r 这个区间内买东西,对于每个物品都尽可能多的买,问你最少剩下多少钱. 析:对于物品,尽可能多的买的意思就是对这个物品 ...

  4. Shell脚本传递带有空格的参数[摘录自网络]

    参数处理 说明 $# 传递到脚本的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$#相同,但是使用时加引号,并在 ...

  5. 书籍索引 #C++

    卷 计算机 的文件夹 PATH 列表卷序列号为 00000200 0001:8890F:.│ 21天学通C++.pdf│ C++ Primer Plus 第6版 中文版.pdf│ C++ Templa ...

  6. 企业搜索引擎开发之连接器connector(十七)

    本文描述连接器的提供与外界交互的servlet接口,连接器与外部是通过xml格式数据交互的 1)  获取所有连接类型 提交地址:http://localhost:8080/connector-mana ...

  7. FIREDAC的TFDJSONDataSets和TFDJSONDeltas查询和提交数据

    服务端代码: uses Data.FireDACJSONReflect, FireDAC.Stan.Storage, FireDAC.Stan.StorageBin, FireDAC.Stan.Sto ...

  8. Android-工作总结-LX-2018-08-20-判断数据库表字段是否为空

    问题的因素: 调试了一上午,我要判断数据库表的name字段是否为空,使用了TextUtils.isEmpty(nameStr):来判断name字段是否为空,明明数据库是没有值,却一直显示有值,然后还去 ...

  9. ASP.NET MVC5实现伪静态

    目录 1.什么是伪静态?为什么要实现伪静态? 2.实现APS.NET MVC伪静态的方式有哪些? 3.那么如何实现使用ASP.NET MVC5的伪静态呢? (1)在路由注册中启用特性路由 (2)为需要 ...

  10. C#学习(1):类型约束

    where T : class泛型类型约束 类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct | T必须是一个结构类型 where T : class T必须是一 ...