剑指offer——面试题32.1:分行从上到下打印二叉树
2024-08-29 00:02:24
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 ;
}
测试代码
最新文章
- eclipse启动tomcat无法访问
- WCF 中 TCP 与 HTTP 性能简单比较
- C#测试题若干,都是基础阿
- NGUI实现技能CD效果
- How to implement updatable view with NHibernate
- emoji和utf8mb4字符集
- 无需添加引用执行JS,发布无需带DLL、例子:QQMD5 QQGTK 13位时间戳 取随机数
- MatOfPoint作为minAreaRect的参数总是报错";throw new IllegalArgumentException(";Incomatible Mat";);
- [apkAnalyzer] 查看APK包名
- Unity 打包完太大
- Linux企业级项目实践之网络爬虫(28)——爬虫socket处理
- PHP引用操作以及外部操作函数的局部静态变量的方法
- 基于规则评分的密码强度检测算法分析及实现(JavaScript)
- JAVA的循环控制与循环嵌套
- dataguard从库移动数据文件
- MobX 学习
- C#邮件发送类 简单实用 可自定义发件人名称
- 环绕声5.1ch
- English trip V1 - B 4.How Do You Make a Salad? 请给我一间房? Teacher:Julia Key:imperatives 祈使句
- mybatisz中一个可以替代between..and 的技巧
热门文章
- [转]Android Parcelable和Serializable的区别
- 12 Overlap Graphs
- C++ 类 &; 对象-类成员函数-类访问修饰符-C++ 友元函数-构造函数 &; 析构函数-C++ 拷贝构造函数
- WCF双工通信笔记
- CString->;char*.,char*->;CString,char*->;LPCTSTR
- CentOS 7更换yum源
- leetcode 有效的字母异位词
- bootstrap table 的searchParam参数传递
- http协议与https协议的区别
- web思维导图(前期)