面试题19:镜像二叉树
2016-07-07 14:34
363 查看
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; }
结果:
相关文章推荐
- 程序员数学——最小二乘法,线性拟合(一)
- Java面试题
- 一个程序员的产品思维
- 海量数据处理面试题
- 程序员应该如何学习英语
- 黑马程序员_Java基础_我的day06学习笔记
- 黑马程序员_Java基础_我的Day05学习笔记
- Android面试 转载
- Java公司面试题集锦(二)
- 剑指Offer:面试题22——栈的压入,弹出序列(java实现)
- 面试题23:二叉树层序遍历
- "Android 布局"-面试必问"精华技能点"汇总.
- 职场困惑——搞大数据的人,还该不该学习代码?
- 程序员的迷茫期
- 职场困惑——还该不该学习代码?
- 程序员都有哪些强迫症行为?
- 剑指Offer:面试题21——包含min函数的栈(java实现)
- Android 面试精华题目总结
- java码农专业技能----在路上就对了
- 【技术晨读】程序员, 不要过度封装!