非递归思路:只需要层序遍历二叉树中每个节点,同时交换每个节点的左右子树即可。
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
// if(root==NULL) return root;
// TreeNode* left=mirrorTree(root->left);
// TreeNode* right=mirrorTree(root->right);
// root->left = right;
// root->right = left;
// return root;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
TreeNode* cur = que.front();
que.pop();
if(cur!=NULL){
TreeNode* temp = cur->left;
cur->left = cur->right;
cur->right = temp;
que.push(cur->left);
que.push(cur->right);
}
}
return root;
}
};