《剑指offer》第三十二题(分行从上到下打印二叉树)
// 面试题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 ;
}
《剑指offer》第三十二题(分行从上到下打印二叉树)的更多相关文章
- 剑指offer——面试题32.1:分行从上到下打印二叉树
void BFSLayer(BinaryTreeNode* pRoot) { if(pRoot==nullptr) return; queue<BinaryTreeNode*> pNode ...
- 剑指Offer(二十二):从上往下打印二叉树
剑指Offer(二十二):从上往下打印二叉树 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...
- 剑指Offer(三十二):把数组排成最小的数
剑指Offer(三十二):把数组排成最小的数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...
- 《剑指offer》第二十二题(链表中倒数第k个结点)
// 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...
- 《剑指offer》第十二题(矩阵中的路径)
// 面试题:矩阵中的路径 // 题目:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有 // 字符的路径.路径可以从矩阵中任意一格开始,每一步可以在矩阵中向左.右. // 上.下移动 ...
- 《剑指offer》第三十二题(不分行从上往下打印二叉树)
// 面试题32(一):不分行从上往下打印二叉树 // 题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印. #include <iostream> #include ...
- 剑指Offer(三十六):两个链表的第一个公共结点
剑指Offer(三十六):两个链表的第一个公共结点 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- 剑指Offer(三十五):数组中的逆序对
剑指Offer(三十五):数组中的逆序对 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...
- 剑指Offer(三十四):第一个只出现一次的字符
剑指Offer(三十四):第一个只出现一次的字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
随机推荐
- [lr] 直方图
直方图基础知识 • 直方图的特征和作用 ▪ 直方图的x轴从左到右代表亮度逐渐增加,即从最暗到最亮:y轴代表某个亮度值下颜色像素的多少(密度). ▪ 直方图由红绿蓝三种颜色组成,分别表示红绿蓝通道:其中 ...
- linux命令:linux文件处理命令
命令格式 : 命令 [-选项] [参数] 例:ls -la /etc 说明:1)个别命令使用不遵循此格式,[]代表可选 2)当有多个选项时,可以写在一起 3)-a等于 --all,调用简化选项用 ...
- c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- toFixed()与toPrecision()
toFixed(n): 返回一个字符串,代表一个以定点表示法表示的数字. n在0~20之间 var g=1.023; var f=g.toFixed(2); f的值为:1.02, typeof ...
- c语言的字符串拷贝函数的精简
#include <stdio.h>#include <string.h>void str_cpy(char * to, char *from){ while ((*to ...
- c#string为传值模式
闲的无聊,记忆中好像是在c/c++语言中string为传址模式(函数修改参数时会影响原来的string参数值),比较好奇c#下对于string参数到底是传值还是传址有些疑问,便亲自测试. 1.结果aa ...
- uva12206 后缀数组
这题说的是给了一串字符 我们要将这个字符 中找出至少出现m次的最长字符串 一个字符课多次使用 利用后缀数组计算最长的lcp 这里有一个点 记得将后缀数组中加入一个空串 如果遇到全部相同的字符时 没办法 ...
- memcache 基础原理
memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访问数据库的网站访问速 ...
- 【JavaScript】数组随机排序 之 Fisher–Yates 洗牌算法
Fisher–Yates随机置乱算法也被称做高纳德置乱算法,通俗说就是生成一个有限集合的随机排列.Fisher-Yates随机置乱算法是无偏的,所以每个排列都是等可能的,当前使用的Fisher-Yat ...
- 怎么归档老日志的shell脚本
本脚本来自有学习阿铭的博文学习:工作中,需要用到日志切割logrotate,按照各自的需要切割.定义保留日志.提示:本文中的S全部都$符,不要问为什么,马云爸爸的社区就这样. #用途:日志切割归档.按 ...