剑指offer——面试题27:二叉树的镜像
函数递归
1 void MirrorIteratively(BinaryTreeNode* pRoot) 2 { 3 if(pRoot == nullptr) 4 return; 5 6 std::stack<BinaryTreeNode*> stackTreeNode; 7 stackTreeNode.push(pRoot); 8 9 while(stackTreeNode.size() > 0) 10 { 11 BinaryTreeNode *pNode = stackTreeNode.top(); 12 stackTreeNode.pop(); 13 14 BinaryTreeNode *pTemp = pNode->m_pLeft; 15 pNode->m_pLeft = pNode->m_pRight; 16 pNode->m_pRight = pTemp; 17 18 if(pNode->m_pLeft) 19 stackTreeNode.push(pNode->m_pLeft); 20 21 if(pNode->m_pRight) 22 stackTreeNode.push(pNode->m_pRight); 23 } 24 }
1 #include"BinaryTree.h" 2 3 // ====================测试代码==================== 4 // 测试完全二叉树:除了叶子节点,其他节点都有两个子节点 5 // 8 6 // 6 10 7 // 5 7 9 11 8 void Test1() 9 { 10 printf("=====Test1 starts:=====\n"); 11 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 12 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 13 BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); 14 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 15 BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 16 BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); 17 BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); 18 19 ConnectTreeNodes(pNode8, pNode6, pNode10); 20 ConnectTreeNodes(pNode6, pNode5, pNode7); 21 ConnectTreeNodes(pNode10, pNode9, pNode11); 22 23 PrintTree(pNode8); 24 25 printf("=====Test1: MirrorRecursively=====\n"); 26 MirrorRecursively(pNode8); 27 PrintTree(pNode8); 28 29 printf("=====Test1: MirrorIteratively=====\n"); 30 MirrorIteratively(pNode8); 31 PrintTree(pNode8); 32 33 DestroyTree(pNode8); 34 } 35 36 // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点 37 // 8 38 // 7 39 // 6 40 // 5 41 // 4 42 void Test2() 43 { 44 printf("=====Test2 starts:=====\n"); 45 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 46 BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 47 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 48 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 49 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 50 51 ConnectTreeNodes(pNode8, pNode7, nullptr); 52 ConnectTreeNodes(pNode7, pNode6, nullptr); 53 ConnectTreeNodes(pNode6, pNode5, nullptr); 54 ConnectTreeNodes(pNode5, pNode4, nullptr); 55 56 PrintTree(pNode8); 57 58 printf("=====Test2: MirrorRecursively=====\n"); 59 MirrorRecursively(pNode8); 60 PrintTree(pNode8); 61 62 printf("=====Test2: MirrorIteratively=====\n"); 63 MirrorIteratively(pNode8); 64 PrintTree(pNode8); 65 66 DestroyTree(pNode8); 67 } 68 69 // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点 70 // 8 71 // 7 72 // 6 73 // 5 74 // 4 75 void Test3() 76 { 77 printf("=====Test3 starts:=====\n"); 78 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 79 BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); 80 BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); 81 BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); 82 BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4); 83 84 ConnectTreeNodes(pNode8, nullptr, pNode7); 85 ConnectTreeNodes(pNode7, nullptr, pNode6); 86 ConnectTreeNodes(pNode6, nullptr, pNode5); 87 ConnectTreeNodes(pNode5, nullptr, pNode4); 88 89 PrintTree(pNode8); 90 91 printf("=====Test3: MirrorRecursively=====\n"); 92 MirrorRecursively(pNode8); 93 PrintTree(pNode8); 94 95 printf("=====Test3: MirrorIteratively=====\n"); 96 MirrorIteratively(pNode8); 97 PrintTree(pNode8); 98 99 DestroyTree(pNode8); 100 } 101 102 // 测试空二叉树:根结点为空指针 103 void Test4() 104 { 105 printf("=====Test4 starts:=====\n"); 106 BinaryTreeNode* pNode = nullptr; 107 108 PrintTree(pNode); 109 110 printf("=====Test4: MirrorRecursively=====\n"); 111 MirrorRecursively(pNode); 112 PrintTree(pNode); 113 114 printf("=====Test4: MirrorIteratively=====\n"); 115 MirrorIteratively(pNode); 116 PrintTree(pNode); 117 } 118 119 // 测试只有一个结点的二叉树 120 void Test5() 121 { 122 printf("=====Test5 starts:=====\n"); 123 BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); 124 125 PrintTree(pNode8); 126 127 printf("=====Test4: MirrorRecursively=====\n"); 128 MirrorRecursively(pNode8); 129 PrintTree(pNode8); 130 131 printf("=====Test4: MirrorIteratively=====\n"); 132 MirrorIteratively(pNode8); 133 PrintTree(pNode8); 134 } 135 136 int main(int argc, char* argv[]) 137 { 138 Test1(); 139 Test2(); 140 Test3(); 141 Test4(); 142 Test5(); 143 144 return 0; 145 }