剑指offer——面试题32.1:分行从上到下打印二叉树
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:分行从上到下打印二叉树的更多相关文章
- 《剑指offer》— JavaScript(22)从上往下打印二叉树
从上往下打印二叉树 题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路 借助两个辅助队列,一个用来存放结点,一个用来存放结点值: 先将根节点加入到队列中,然后遍历队列中的元素,遍历 ...
- 《剑指offer》第三十二题(分行从上到下打印二叉树)
// 面试题32(二):分行从上到下打印二叉树 // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层 // 打印到一行. #include <cstdio> #in ...
- 《剑指offer》第三十二题(不分行从上往下打印二叉树)
// 面试题32(一):不分行从上往下打印二叉树 // 题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印. #include <iostream> #include ...
- 《剑指offer》第三十二题(之字形打印二叉树)
// 面试题32(三):之字形打印二叉树 // 题目:请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺 // 序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印, / ...
- 【剑指offer】不分行从上到下打印二叉树,C++实现(层序遍历)
原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目 从上往下打印出二叉树的每个节点,同层节点从左至右打印.例如: 图 不分行从上往下按层打印二叉 ...
- 剑指Offer:面试题32——从1到n整数中1出现的次数(java实现)
问题描述: 输入一个整数n,求1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11,12,1一共出现了5次. 思路:(不考虑时间效率的解法,肯定不 ...
- 剑指offer——面试题32:从上到下打印二叉树
void BFS(BinaryTreeNode* pRoot) { if(pRoot==nullptr) { cout<<"empty binary tree!"< ...
- 剑指offer 分行从上到下打印二叉树
题目: 从上到下按层打印二叉树,同一层的节点按照从左到右的顺序打印,每一层打印到一行. /* struct TreeNode { int val; struct TreeNode *left; str ...
- 剑指offer——33分行从上到下打印二叉树
题目描述 从上到下按层打印二叉树,同一层结点从左至右输出.每一层输出一行. 题解: 使用BFS,按层打印即可 class Solution { public: vector<vector&l ...
随机推荐
- 从Objective-C到Swift,你必须会的(一)#pragma mark
在Objective-C里,为了让代码组织的有序也方便用control+6的快捷键在Xcode中查找,所以出现了一个大家都很熟悉的东东.这就是:#prama mark. #pragma mark 但 ...
- javascript与java的相互调用,纯java的javascript引擎rhino(转载)
1.下载Rhino安装包,下载地址:官网http://www.mozilla.org/rhino. 2.rhino环境配置,把解压出来的js.jar文件加入到系统的环境变量classpath 3.在命 ...
- LNMP详细介绍
1>Nginx概述: 很多人对apache非常熟悉,Nginx与Apache类似,属于WEB容器,同时也是一款高性能的HTTP和反向代理软件,它们之间最大的差别是Apache的处理速 ...
- 在ASP.NET Core2上操作MongoDB就是能这么的简便酷爽(自动完成分库分表)
NoSQL是泛指非关系型的数据库,现今在我们的项目中也多有使用,其独特的优点为我们的项目架构带来了不少亮点,而我们这里的主角(MongoDB)则是NoSQL数据库家族中的一种.事实上,NoSQL数据库 ...
- Mahout的taste里的几种相似度计算方法
欧几里德相似度(Euclidean Distance) 最初用于计算欧几里德空间中两个点的距离,以两个用户x和y为例子,看成是n维空间的两个向量x和y, xi表示用户x对itemi的喜好值,yi表示 ...
- Azure静态公网ip自助反解
Linux下安装az工具,并登陆 az login 执行 az network public-ip update --resource-group ip所在资源组名称 --name ip对应资源名称 ...
- Jenkins RestAPI调用出现Error 403 No valid crumb was included in the request
方法一(不推荐): 在jenkins 的Configure Global Security下 , 取消“防止跨站点请求伪造(Prevent Cross Site Request Forgery exp ...
- 3:C#异步WaitAll的使用
编写界面如图: private async void button1_Click(object sender, EventArgs e) { #region 单个执行的异步,效率慢 HttpClien ...
- pageadmin CMS网站建设教程:网页设计的常用参数
由于网络速度问题,我们需要考虑图片大小对传输速度的影响,如果图片太大就会影响浏览速度,访问者很快就会对这个网站失去了兴趣,只有充分了解图片质量与下载速度的关系,并了解不同的文件格式,才能更有效的表达内 ...
- Java50道经典习题-程序16 在控制台上打印九九乘法表
题目:输出9*9口诀.分析:利用双重for循环进行输出,分行与列考虑,共9行9列,i控制行,j控制列. public class Prog16 { public static void main(St ...