《剑指offer》第三十二题(之字形打印二叉树)
// 面试题32(三):之字形打印二叉树
// 题目:请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺
// 序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,
// 其他行以此类推。 #include <iostream>
#include "BinaryTree.h"
#include <stack> void Print(BinaryTreeNode* pRoot)
{
if (pRoot == nullptr)
return; std::stack<BinaryTreeNode*> levels[];//两个栈
int current = ;//设置两个0/1分布但不等的值,用来轮到作为当前和下个栈
int next = ; levels[current].push(pRoot);
while (!levels[].empty() || !levels[].empty())
{
BinaryTreeNode* pNode = levels[current].top();
levels[current].pop(); printf("%d ", pNode->m_nValue); if (current == )//决定先左还是先右
{
if (pNode->m_pLeft != nullptr)
levels[next].push(pNode->m_pLeft);
if (pNode->m_pRight != nullptr)
levels[next].push(pNode->m_pRight);
}
else
{
if (pNode->m_pRight != nullptr)
levels[next].push(pNode->m_pRight);
if (pNode->m_pLeft != nullptr)
levels[next].push(pNode->m_pLeft);
} if (levels[current].empty())//如果当前栈空了,就换一下栈的序号
{
printf("\n");
current = - current;
next = - next;
}
}
} // ====================测试代码====================
// 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("10 6 \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");
} // 8
// 4 12
// 2 6 10 14
// 1 3 5 7 9 11 13 15
void Test7()
{
BinaryTreeNode* pNode8 = CreateBinaryTreeNode();
BinaryTreeNode* pNode4 = CreateBinaryTreeNode();
BinaryTreeNode* pNode12 = CreateBinaryTreeNode();
BinaryTreeNode* pNode2 = CreateBinaryTreeNode();
BinaryTreeNode* pNode6 = CreateBinaryTreeNode();
BinaryTreeNode* pNode10 = CreateBinaryTreeNode();
BinaryTreeNode* pNode14 = CreateBinaryTreeNode();
BinaryTreeNode* pNode1 = CreateBinaryTreeNode();
BinaryTreeNode* pNode3 = CreateBinaryTreeNode();
BinaryTreeNode* pNode5 = CreateBinaryTreeNode();
BinaryTreeNode* pNode7 = CreateBinaryTreeNode();
BinaryTreeNode* pNode9 = CreateBinaryTreeNode();
BinaryTreeNode* pNode11 = CreateBinaryTreeNode();
BinaryTreeNode* pNode13 = CreateBinaryTreeNode();
BinaryTreeNode* pNode15 = CreateBinaryTreeNode(); ConnectTreeNodes(pNode8, pNode4, pNode12);
ConnectTreeNodes(pNode4, pNode2, pNode6);
ConnectTreeNodes(pNode12, pNode10, pNode14);
ConnectTreeNodes(pNode2, pNode1, pNode3);
ConnectTreeNodes(pNode6, pNode5, pNode7);
ConnectTreeNodes(pNode10, pNode9, pNode11);
ConnectTreeNodes(pNode14, pNode13, pNode15); printf("====Test7 Begins: ====\n");
printf("Expected Result is:\n");
printf("8 \n");
printf("12 4 \n");
printf("2 6 10 14 \n");
printf("15 13 11 9 7 5 3 1 \n\n"); printf("Actual Result is: \n");
Print(pNode8);
printf("\n"); DestroyTree(pNode8);
} int main(int argc, char* argv[])
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
system("pause");
return ;
}
《剑指offer》第三十二题(之字形打印二叉树)的更多相关文章
- 剑指Offer(三十二):把数组排成最小的数
剑指Offer(三十二):把数组排成最小的数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...
- 《剑指offer》第二十九题(顺时针打印矩阵)
// 面试题29:顺时针打印矩阵 // 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. #include <iostream> void PrintMatrixInC ...
- 《剑指offer》第二十二题(链表中倒数第k个结点)
// 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...
- 《剑指offer》第十二题(矩阵中的路径)
// 面试题:矩阵中的路径 // 题目:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有 // 字符的路径.路径可以从矩阵中任意一格开始,每一步可以在矩阵中向左.右. // 上.下移动 ...
- 《剑指offer》第二十八题(对称的二叉树)
// 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...
- 剑指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 ...
- 剑指Offer(三十):连续子数组的最大和
.# 剑指Offer(三十):连续子数组的最大和 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- 剑指Offer - 九度1523 - 从上往下打印二叉树
剑指Offer - 九度1523 - 从上往下打印二叉树2013-12-01 00:35 题目描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 输入: 输入可能包含多个测试样例,输入以E ...
随机推荐
- php mysql_connect pmysql_connect区别
<?php mysql_pconnect('192.168.75.128', 'root', 'root'); mysql_close(); sleep ...
- mysql服务器上的mysql这个实例中表的介绍
1.user表. 分个分隔符
- 何为仿射变换(Affine Transformation)
http://www.cnblogs.com/ghj1976/p/5199086.html 变换模型是指根据待匹配图像与背景图像之间几何畸变的情况,所选择的能最佳拟合两幅图像之间变化的几何变换模型.可 ...
- cocoapods 配置
二.CocoaPods 安装 CocoaPods可以方便地通过Mac自带的RubyGems安装. 打开Terminal(Mac电脑自带的终端): (1).设置ruby的软件源 这是因为ruby的软件源 ...
- FastReport问题整理(转)
FastReport问题整理 博客分类: 软件开发 部分来自网上,部分来自网友,部分来自Demo如果有新的内容,会不断更新.. 更新历史: 2009-02-27 加入套打方案全攻略(原:jinzh ...
- linux常用命令:ss 命令
ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...
- Windows10 64位下安装TensorFlow谷歌人工智能系统已官方原生支持
Windows10 64位下安装TensorFlow谷歌人工智能系统已官方原生支持 GitHub - tensorflow/tensorflow: Computation using data flo ...
- DeepMind已将AlphaGo引入多领域 Al泡沫严重
DeepMind已将AlphaGo引入多领域 Al泡沫严重 在稳操胜券的前提下,谷歌旗下的AlphaGo还是向柯洁下了战书.4月10日,由中国围棋协会.浙江省体育局.谷歌三方联合宣布,将于5月23日至 ...
- javascript闭包(Module模式)的用途和高级使用方式
javascript闭包(Module模式)的用途和高级使用方式 javascript闭包的用途:1. 匿名自执行函数:或者可以理解为,避免污染全局变量2. 缓存:源于闭包的核心特性便是保存状态,应用 ...
- web应用下的安全问题以及tomcat/nginx对应解决方法(持续更新、亲测可解决问题)
最近一券商那边扫描反馈了下面几个非业务型安全漏洞,要求解决,如下: XSS 自己写个脚本response的时候对特殊字符进行了处理,或者网上搜下一堆(不要忘了回车.换行). HTML form wit ...