JZ-C-19
剑指offer第十九题:二叉树的镜像
1 //============================================================================ 2 // Name : JZ-C-19.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : 二叉树的镜像 7 //============================================================================ 8 9 #include <iostream> 10 #include <stdio.h> 11 #include <stack> 12 #include "BinaryTree.h" 13 using namespace std; 14 /** 15 *前序遍历:递归 16 */ 17 void MirrorRecursively(BinaryTreeNode* root) { 18 if (root == NULL) { 19 return; 20 } 21 if (root->m_pLeft == NULL && root->m_pRight == NULL) { //这里是&& 22 return; 23 } 24 BinaryTreeNode* temp = root->m_pLeft; 25 ; //临时结点,用于交换左右子孩子 26 root->m_pLeft = root->m_pRight; 27 root->m_pRight = temp; 28 if (root->m_pLeft != NULL) { 29 MirrorRecursively(root->m_pLeft); 30 } 31 if (root->m_pRight != NULL) { 32 MirrorRecursively(root->m_pRight); 33 } 34 } 35 /** 36 *前序遍历:循环 37 */ 38 void MirrorIteratively(BinaryTreeNode *root) { 39 if (root == NULL) { 40 return; 41 } 42 std::stack<BinaryTreeNode*> stackTreeNode; //利用栈的出入来存储每次需要遍历的结点 43 stackTreeNode.push(root); 44 45 while (stackTreeNode.size() > 0) { 46 BinaryTreeNode *Node = stackTreeNode.top(); //拿到要操作的结点 47 stackTreeNode.pop(); //出栈 48 49 BinaryTreeNode *temp = Node->m_pLeft; 50 Node->m_pLeft = Node->m_pRight; 51 Node->m_pRight = temp; 52 53 if (Node->m_pLeft != NULL) { 54 stackTreeNode.push(Node->m_pLeft); 55 } 56 if (Node->m_pRight != NULL) { 57 stackTreeNode.push(Node->m_pRight); 58 } 59 } 60 } 61 // ====================测试代码==================== 62 // 测试完全二叉树:除了叶子节点,其他节点都有两个子节点 63 // 8 64 // 6 10 65 // 5 7 9 11 66 void Test1() { 67 printf("=====Test1 starts:=====\n"); 68 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 69 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 70 BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 71 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 72 BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 73 BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 74 BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 75 76 ConnectTreeNodes(pNode8, pNode6, pNode10); 77 ConnectTreeNodes(pNode6, pNode5, pNode7); 78 ConnectTreeNodes(pNode10, pNode9, pNode11); 79 80 PrintTree(pNode8); 81 82 printf("=====Test1: MirrorRecursively=====\n"); 83 MirrorRecursively(pNode8); 84 PrintTree(pNode8); 85 86 printf("=====Test1: MirrorIteratively=====\n"); 87 MirrorIteratively(pNode8); 88 PrintTree(pNode8); 89 90 DestroyTree(pNode8); 91 } 92 93 // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点 94 // 8 95 // 7 96 // 6 97 // 5 98 // 4 99 void Test2() { 100 printf("=====Test2 starts:=====\n"); 101 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 102 BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 103 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 104 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 105 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 106 107 ConnectTreeNodes(pNode8, pNode7, NULL); 108 ConnectTreeNodes(pNode7, pNode6, NULL); 109 ConnectTreeNodes(pNode6, pNode5, NULL); 110 ConnectTreeNodes(pNode5, pNode4, NULL); 111 112 PrintTree(pNode8); 113 114 printf("=====Test2: MirrorRecursively=====\n"); 115 MirrorRecursively(pNode8); 116 PrintTree(pNode8); 117 118 printf("=====Test2: MirrorIteratively=====\n"); 119 MirrorIteratively(pNode8); 120 PrintTree(pNode8); 121 122 DestroyTree(pNode8); 123 } 124 125 // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点 126 // 8 127 // 7 128 // 6 129 // 5 130 // 4 131 void Test3() { 132 printf("=====Test3 starts:=====\n"); 133 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 134 BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 135 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 136 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 137 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 138 139 ConnectTreeNodes(pNode8, NULL, pNode7); 140 ConnectTreeNodes(pNode7, NULL, pNode6); 141 ConnectTreeNodes(pNode6, NULL, pNode5); 142 ConnectTreeNodes(pNode5, NULL, pNode4); 143 144 PrintTree(pNode8); 145 146 printf("=====Test3: MirrorRecursively=====\n"); 147 MirrorRecursively(pNode8); 148 PrintTree(pNode8); 149 150 printf("=====Test3: MirrorIteratively=====\n"); 151 MirrorIteratively(pNode8); 152 PrintTree(pNode8); 153 154 DestroyTree(pNode8); 155 } 156 157 // 测试空二叉树:根结点为空指针 158 void Test4() { 159 printf("=====Test4 starts:=====\n"); 160 BinaryTreeNode* pNode = NULL; 161 162 PrintTree(pNode); 163 164 printf("=====Test4: MirrorRecursively=====\n"); 165 MirrorRecursively(pNode); 166 PrintTree(pNode); 167 168 printf("=====Test4: MirrorIteratively=====\n"); 169 MirrorIteratively(pNode); 170 PrintTree(pNode); 171 } 172 173 // 测试只有一个结点的二叉树 174 void Test5() { 175 printf("=====Test5 starts:=====\n"); 176 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 177 178 PrintTree(pNode8); 179 180 printf("=====Test4: MirrorRecursively=====\n"); 181 MirrorRecursively(pNode8); 182 PrintTree(pNode8); 183 184 printf("=====Test4: MirrorIteratively=====\n"); 185 MirrorIteratively(pNode8); 186 PrintTree(pNode8); 187 } 188 189 int main(int argc, char** argv) { 190 Test1(); 191 Test2(); 192 Test3(); 193 Test4(); 194 Test5(); 195 196 return 0; 197 }
—————————————————————————————————————行走在人猿的并行线——Laughing_Lz