二叉树的镜像变换
1 #include "stdafx.h" 2 #include <iostream> 3 using namespace std; 4 5 struct BinaryTreeNode 6 { 7 int m_nValue; 8 BinaryTreeNode *m_pLeft; 9 BinaryTreeNode *m_pRight; 10 }; 11 12 //构造树的镜像 13 void Mirror(BinaryTreeNode *pRoot) 14 { 15 if (pRoot != NULL) 16 { 17 BinaryTreeNode *pTemp = NULL; 18 if (pRoot->m_pLeft != NULL || pRoot->m_pRight != NULL) 19 { 20 pTemp = pRoot->m_pLeft; 21 pRoot->m_pLeft = pRoot->m_pRight; 22 pRoot->m_pRight = pTemp; 23 } 24 25 if (pRoot->m_pLeft != NULL) 26 { 27 Mirror(pRoot->m_pLeft); 28 } 29 30 if (pRoot->m_pRight != NULL) 31 { 32 Mirror(pRoot->m_pRight); 33 } 34 } 35 } 36 37 //以先序的方式构建二叉树,输入-1表示结点为空 38 void CreateBinaryTree(BinaryTreeNode *&pRoot) 39 { 40 int nNodeValue = 0; 41 cin >> nNodeValue; 42 if (-1 == nNodeValue) 43 { 44 return; 45 } 46 else 47 { 48 pRoot = new BinaryTreeNode(); 49 pRoot->m_nValue = nNodeValue; 50 CreateBinaryTree(pRoot->m_pLeft); 51 CreateBinaryTree(pRoot->m_pRight); 52 } 53 } 54 55 void PrintInOrder(BinaryTreeNode *&pRoot) 56 { 57 if (pRoot != NULL) 58 { 59 PrintInOrder(pRoot->m_pLeft); 60 cout << pRoot->m_nValue << " "; 61 PrintInOrder(pRoot->m_pRight); 62 } 63 } 64 65 int _tmain(int argc, _TCHAR* argv[]) 66 { 67 BinaryTreeNode *pRoot = NULL; 68 CreateBinaryTree(pRoot); 69 PrintInOrder(pRoot); 70 cout << endl; 71 72 Mirror(pRoot); 73 PrintInOrder(pRoot); 74 cout << endl; 75 76 system("pause"); 77 return 0; 78 }
1 //二叉树的镜像变换 2 #include <iostream> 3 #include <stack> 4 using namespace std; 5 6 //二叉树结构 7 struct BTreeNode 8 { 9 int m_nValue; 10 BTreeNode *m_pLeft; 11 BTreeNode *m_pRight; 12 }; 13 14 //递归实现 15 void MirrorRecursively(BTreeNode *pNode) 16 { 17 if(!pNode) 18 { 19 return; 20 } 21 //交换左右孩子 22 BTreeNode *pTemp = pNode->m_pLeft; 23 pNode->m_pLeft = pNode->m_pRight; 24 pNode->m_pRight = pTemp; 25 26 if(pNode->m_pLeft) 27 { 28 MirrorRecursively(pNode->m_pLeft); 29 } 30 31 if(pNode->m_pRight) 32 { 33 MirrorRecursively(pNode->m_pRight); 34 } 35 } 36 37 void MirrorIteratively(BTreeNode *pNode) 38 { 39 if(!pNode) 40 { 41 return ; 42 } 43 44 stack<BTreeNode *> stackTreeNode; 45 stackTreeNode.push(pNode); 46 47 while(stackTreeNode.size()) 48 { 49 BTreeNode *pNode = stackTreeNode.top(); 50 stackTreeNode.pop(); 51 52 //交换左右孩子 53 BTreeNode *pTemp = pNode->m_pLeft; 54 pNode->m_pLeft = pNode->m_pRight; 55 pNode->m_pRight = pTemp; 56 57 if(pNode->m_pLeft) 58 { 59 stackTreeNode.push(pNode->m_pLeft); 60 } 61 if(pNode->m_pRight) 62 { 63 stackTreeNode.push(pNode->m_pRight); 64 } 65 } 66 67 } 68 69 //按照先序创建二叉树,0表示空 70 BTreeNode *Create() 71 { 72 int ch; 73 cin>> ch; 74 if(ch == 0) 75 { 76 return NULL; 77 } 78 else 79 { 80 BTreeNode *root = new BTreeNode(); 81 root->m_nValue = ch; 82 root->m_pLeft = Create(); 83 root->m_pRight = Create(); 84 return root; 85 } 86 } 87 88 //按照先序 打印 树 89 void printBTree(BTreeNode *root) 90 { 91 if(!root) 92 { 93 return ; 94 } 95 cout << root->m_nValue <<" "; 96 printBTree(root->m_pLeft); 97 printBTree(root->m_pRight); 98 } 99 100 void main() 101 { 102 BTreeNode *tree = NULL; 103 cout << "先序创建二叉树:"; 104 tree = Create(); 105 cout << "先序打印二叉树:"; 106 printBTree(tree); 107 108 MirrorRecursively(tree); 109 cout <<endl<<"变换后:"; 110 printBTree(tree); 111 112 cout <<endl<<"再次变换:"; 113 MirrorIteratively(tree); 114 printBTree(tree); 115 cout << endl; 116 }