迭代法+递归法
二叉树的镜像
http://www.nowcoder.com/questionTerminal/a9d0ecbacef9410ca97463e4a5c83be7
class Solution { public: TreeNode* Mirror(TreeNode* pRoot) { if(!pRoot) return nullptr; //使用一个stack stack<TreeNode*> stackTreeNode; stackTreeNode.push(pRoot); while(!stackTreeNode.empty()){ TreeNode* pNode=stackTreeNode.top(); stackTreeNode.pop(); TreeNode* pTmp=pNode->left; pNode->left=pNode->right; pNode->right=pTmp; if(pNode->left) stackTreeNode.push(pNode->left); if(pNode->right) stackTreeNode.push(pNode->right); } return pRoot; } }; class Solution { public: TreeNode* Mirror(TreeNode* pRoot) { if(!pRoot) return nullptr; if(!pRoot->left && !pRoot->right) return pRoot; TreeNode* pTmp=pRoot->left; pRoot->left=pRoot->right; pRoot->right=pTmp; if(pRoot->left) Mirror(pRoot->left); if(pRoot->right) Mirror(pRoot->right); return pRoot; } };