For example, the following two regions are images of each other.
Idea: traverse each node of the tree in sequence. If the node to be traversed has a subnode, it will exchange two nodes.
# Include "stdafx. h "# include <iostream> using namespace std; struct BinaryTreeNode {int m_nValue; BinaryTreeNode * m_pLeft; BinaryTreeNode * m_pRight ;}; // construct the image void Mirror (BinaryTreeNode * pRoot) {if (pRoot! = NULL) {BinaryTreeNode * pTemp = NULL; if (pRoot-> m_pLeft! = NULL | pRoot-> m_pRight! = NULL) {pTemp = pRoot-> m_pLeft; pRoot-> m_pLeft = pRoot-> m_pRight; pRoot-> m_pRight = pTemp;} if (pRoot-> m_pLeft! = NULL) {Mirror (pRoot-> m_pLeft);} if (pRoot-> m_pRight! = NULL) {Mirror (pRoot-> m_pRight) ;}}// create a binary tree in the First Order. Input-1 to indicate that the node is empty void CreateBinaryTree (BinaryTreeNode * & pRoot) {int nNodeValue = 0; cin> nNodeValue; if (-1 = nNodeValue) {return;} else {pRoot = new BinaryTreeNode (); pRoot-> m_nValue = nNodeValue; createBinaryTree (pRoot-> m_pLeft); CreateBinaryTree (pRoot-> m_pRight) ;}} void PrintInOrder (BinaryTreeNode * & pRoot) {if (pRoot! = NULL) {PrintInOrder (pRoot-> m_pLeft); cout <pRoot-> m_nValue <""; PrintInOrder (pRoot-> m_pRight );}} int _ tmain (int argc, _ TCHAR * argv []) {BinaryTreeNode * pRoot = NULL; CreateBinaryTree (pRoot); PrintInOrder (pRoot); cout <endl; mirror (pRoot); PrintInOrder (pRoot); cout <endl; system ("pause"); return 0 ;}