树的镜像
来源:互联网 发布:手机能不能做淘宝客服 编辑:程序博客网 时间:2024/05/16 10:13
题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ 11 9 7 5
#include <iostream>#include "BinaryTree.h"using namespace std;void MirrorOfBinaryTree(BinaryTreeNode* pRoot){if(pRoot == NULL || (pRoot ->m_pLeft == NULL && pRoot ->m_pRight))return;BinaryTreeNode* bTemp = pRoot ->m_pLeft;pRoot ->m_pLeft = pRoot ->m_pRight;pRoot ->m_pRight = bTemp;if(pRoot->m_pLeft != NULL)MirrorOfBinaryTree(pRoot ->m_pLeft);if(pRoot ->m_pRight != NULL)MirrorOfBinaryTree(pRoot ->m_pRight);}
// ====================测试代码====================// 测试完全二叉树:除了叶子节点,其他节点都有两个子节点// 8// 6 10// 5 7 9 11void Test1(){ printf("=====Test1 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); ConnectTreeNodes(pNode8, pNode6, pNode10); ConnectTreeNodes(pNode6, pNode5, pNode7); ConnectTreeNodes(pNode10, pNode9, pNode11); PrintTree(pNode8); printf("=====Test1: MirrorRecursively=====\n"); MirrorOfBinaryTree(pNode8); PrintTree(pNode8); DestroyTree(pNode8);}// 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点// 8// 7 // 6 // 5// 4void Test2(){ printf("=====Test2 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); ConnectTreeNodes(pNode8, pNode7, NULL); ConnectTreeNodes(pNode7, pNode6, NULL); ConnectTreeNodes(pNode6, pNode5, NULL); ConnectTreeNodes(pNode5, pNode4, NULL); PrintTree(pNode8); printf("=====Test2: MirrorRecursively=====\n"); MirrorOfBinaryTree(pNode8); PrintTree(pNode8); DestroyTree(pNode8);}// 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点// 8// 7 // 6 // 5// 4void Test3(){ printf("=====Test3 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); ConnectTreeNodes(pNode8, NULL, pNode7); ConnectTreeNodes(pNode7, NULL, pNode6); ConnectTreeNodes(pNode6, NULL, pNode5); ConnectTreeNodes(pNode5, NULL, pNode4); PrintTree(pNode8); printf("=====Test3: MirrorRecursively=====\n"); MirrorOfBinaryTree(pNode8); PrintTree(pNode8); DestroyTree(pNode8);}// 测试空二叉树:根结点为空指针void Test4(){ printf("=====Test4 starts:=====\n"); BinaryTreeNode* pNode = NULL; PrintTree(pNode); printf("=====Test4: MirrorRecursively=====\n"); MirrorOfBinaryTree(pNode); PrintTree(pNode);}// 测试只有一个结点的二叉树void Test5(){ printf("=====Test5 starts:=====\n"); BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); PrintTree(pNode8); printf("=====Test4: MirrorRecursively=====\n"); MirrorOfBinaryTree(pNode8); PrintTree(pNode8);}int main(int argc, char* argv[]){ Test1(); Test2(); Test3(); Test4(); Test5(); return 0;}
0 0
- 树的镜像
- 树的镜像
- 树的镜像
- 树的镜像
- 树的镜像
- 树的镜像
- 树的镜像
- 树的镜像反转
- 树的镜像反转
- 二叉树的镜像
- 二叉树的镜像
- 二叉树的镜像
- 二叉树的镜像
- 二叉树的镜像
- 二叉树的镜像
- 二叉树的镜像
- 二叉树的镜像
- 二叉树的镜像
- swift 错误处理
- Shiny应用基础(5):数据获取与响应
- C# WPF数据绑定
- 常用的mime-mapping配置
- Android-在Android studio中实现远程服务(Service)AIDL
- 树的镜像
- 代码实现List下载列表(安卓自定义控件,List列表)
- C# 窗体程序 DataGridView 输出换行
- BZOJ 题目3172: [Tjoi2013]单词(AC自动机||AC自动机+fail树||后缀数组暴力||后缀数组+RMQ+二分等五种姿势水过)
- Xamarin.Forms 之简单介绍
- 进入MAC下面的Library目录
- PS中,如何给文字填充成渐变色
- ubuntu环境下tftp安装
- Linux C中的opendir()