// 面试题32(二):分行从上到下打印二叉树
// 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层
// 打印到一行。 #include <cstdio>
#include "BinaryTree.h"
#include <queue> void Print(BinaryTreeNode* pRoot)
{
if (pRoot == nullptr)
return; std::queue<BinaryTreeNode*> nodes;
nodes.push(pRoot);
int nextLevel = ;//相比上个题,多了两个变量,一个是下层结点个数,初始化为0
int toBePrinted = ;//一个是待打印的点,初始化为1
while (!nodes.empty())
{
BinaryTreeNode* pNode = nodes.front();
printf("%d ", pNode->m_nValue); if (pNode->m_pLeft != nullptr)
{
nodes.push(pNode->m_pLeft);
++nextLevel;//每有一个孩子进入队列,下层结点加1
}
if (pNode->m_pRight != nullptr)
{
nodes.push(pNode->m_pRight);
++nextLevel;
} nodes.pop();
--toBePrinted;//每弹出一个节点,待打印节点就减1
if (toBePrinted == )//直到当前层打印完
{
printf("\n");//换行
toBePrinted = nextLevel;//把记住的下层结点总数给它
nextLevel = ;//下层结点要打印的个数置零,从新计数
}
}
} // ====================测试代码====================
// 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");
Print(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");
Print(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");
Print(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");
Print(pNode5);
printf("\n"); DestroyTree(pNode5);
} void Test5()
{
printf("====Test5 Begins: ====\n");
printf("Expected Result is:\n"); printf("Actual Result is: \n");
Print(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");
Print(pNode100);
printf("\n");
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
system("pause");
return ;
}

最新文章

  1. windows charles response 乱码解决办法
  2. kmdjs指令大全
  3. 最常用的ES6特性
  4. C#编程总结 字符转码
  5. Python 基礎 - 數據類型
  6. 不均匀的Windows处理器编组
  7. A Gentle Introduction to the Gradient Boosting Algorithm for Machine Learning
  8. C++实现一个限制对象实例个数的类
  9. Topcoder SRM 661 (Div.1) 250 MissingLCM - 数论
  10. QQ弹窗代码
  11. eclipse如何运行maven项目
  12. 【OC加强】辛格尔顿和[NSFileManager defaultMagager]以及其他设计模式
  13. 深入探索C++对象模型(五)
  14. ue4 材质表达式分类
  15. localhost无法访问的问题
  16. win7文件夹带锁标志如何去除?win7去除文件夹带锁标志的方法
  17. 洛谷 P1135 奇怪的电梯 【基础BFS】
  18. 4.2计算字符的ASCII碼
  19. python——ADSL拨号程序
  20. Spring学习笔记1——基础知识

热门文章

  1. Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数
  2. STA分析(六) cross talk and noise
  3. Yii2 配置 Nginx 伪静态
  4. JSON自动生成相关类
  5. Linux基础命令---tune2fs
  6. python基础八---文件操作
  7. JavaScript甜点(1)
  8. P3501 [POI2010]ANT-Antisymmetry
  9. PN结讲解
  10. Beetl模板引擎入门教程