题目描述
https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
迭代版本
class Solution {
public:
TreeNode* mirrorTree(TreeNode* pRoot) {
queue<TreeNode*> q;
if(pRoot) q.push(pRoot);
while(q.size())
{
TreeNode* t=q.front();
q.pop();
swap(t->left,t->right);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
return pRoot;
}
};
递归版本:
class Solution {
public:
TreeNode* Mirror(TreeNode* pRoot) {
if(!pRoot) return nullptr;
swap(pRoot->left,pRoot->right);
Mirror(pRoot->left);
Mirror(pRoot->right);
return pRoot;
}
};