您的位置:首页 > 职场人生

面试题19:镜像二叉树

2016-07-07 14:34 288 查看
1.输入一棵二叉树,输出它的镜像

图示:



分析:递归的交换左右子树,如图示,



源码:
/*二叉树镜像*/
#include <iostream>

using namespace std;

#define MAXLEN 10
#define NLAYER  4
#define INF 127

int PrintTree_h_height;
char PrintTree_h_buffer[6][128];
int PrintTree_h_x;

struct BinaryTreeNode
{
int                    m_nValue;
BinaryTreeNode*        m_pLeft;
BinaryTreeNode*        m_pRight;
};
//创建二叉树结点
BinaryTreeNode* CreateBinaryTreeNode(int value)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pLeft = NULL;
pNode->m_pRight = NULL;

return pNode;
}
//连接三个结点
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if (pParent != NULL)
{
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
}
//树高
int GetTreeHeight(BinaryTreeNode* pNode)
{
if (pNode == NULL)
{
return 0;
}
int heightLeft = GetTreeHeight(pNode->m_pLeft);
int heightRight = GetTreeHeight(pNode->m_pRight);

if (heightLeft > heightRight)
return heightLeft + 1;
else
return heightRight + 1;

}

//打印树节点
void PrintTreeNode(BinaryTreeNode* pNode)
{
if (pNode != NULL)
{

cout << "value of this node is: " << pNode->m_nValue << endl;

if (pNode->m_pLeft != NULL)

cout << "value of its left child is: " << pNode->m_pLeft->m_nValue << endl;
else

cout << "left child is null.\n";

if (pNode->m_pRight != NULL)

cout << "value of its right child is:" << pNode->m_pRight->m_nValue << endl;
else

cout << "right child is null.\n";
}
else
{
cout << "this node is null.\n";
}
cout << endl;

}
//打印二叉树
void PrintTree(BinaryTreeNode* pRoot)
{
PrintTreeNode(pRoot);

if (pRoot != NULL)
{
if (pRoot->m_pLeft != NULL)
PrintTree(pRoot->m_pLeft);

if (pRoot->m_pRight != NULL)
PrintTree(pRoot->m_pRight);
}
}
//销毁二叉树
void DestroyTree(BinaryTreeNode* pRoot)
{
if (pRoot != NULL)
{
BinaryTreeNode* pLeft = pRoot->m_pLeft;
BinaryTreeNode* pRight = pRoot->m_pRight;

delete pRoot;
pRoot = NULL;

DestroyTree(pLeft);
DestroyTree(pRight);
}
}

void PrintTree_h_corePrintTree(BinaryTreeNode* tree, int level){
if (tree == NULL){
PrintTree_h_x += (pow(2, PrintTree_h_height - level) - 1);
return;
}
char(*a)[128] = PrintTree_h_buffer;
PrintTree_h_corePrintTree(tree->m_pLeft, level + 1);
a[level][PrintTree_h_x++] = tree->m_nValue;
PrintTree_h_corePrintTree(tree->m_pRight, level + 1);
}

//树形打印二叉树
void ShowTree(BinaryTreeNode* tree)
{

if (tree == NULL) return;
char(*a)[128] = PrintTree_h_buffer;
for (int i = 0; i < 6; i++){
for (int j = 0; j<128; j++){
a[i][j] = INF;
}
}
//先获取树高度
PrintTree_h_height = GetTreeHeight(tree);
if (PrintTree_h_height > 6){
cout << "树超过6层,无法打印" << endl;
return;
}
PrintTree_h_corePrintTree(tree, 0);
for (int i = 0; i < PrintTree_h_height; i++){//按实际高度打印
for (int j = 0; j < 128; j++){
if (a[i][j] == INF) cout << " ";
else cout << (int)a[i][j];
}
cout << endl;
}
}

//二叉树镜像-递归的方法
void MirrorBinaryTree(BinaryTreeNode* pNode)
{
if (pNode == NULL || (pNode->m_pLeft == NULL&&pNode->m_pRight == NULL))
{
return;
}
BinaryTreeNode *pTemp = pNode->m_pLeft;
pNode->m_pLeft = pNode->m_pRight;
pNode->m_pRight = pTemp;

if (pNode->m_pLeft)
MirrorBinaryTree(pNode->m_pLeft);

if (pNode->m_pRight)
MirrorBinaryTree(pNode->m_pRight);
}

int main()
{
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);

cout << "the original tree:" << endl;
ShowTree(pNode8);
cout << "after mirror:" << endl;
MirrorBinaryTree(pNode8);
ShowTree(pNode8);
DestroyTree(pNode8);

system("PAUSE");
return 0;
}


结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: