递归法
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Mirror(root)
{
if (!root) return root;
let left = Mirror(root.left);
let right = Mirror(root.right);
root.left = right;
root.right = left;
return root;
}
module.exports = {
Mirror : Mirror
}; BFS
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Mirror(root)
{
if (!root) return root;
let queue = [root];
while (queue.length) {
let len = queue.length;
while (len--) {
let node = queue.shift();
node.left && queue.push(node.left);
node.right && queue.push(node.right);
// 交换左右
let temp = node.left;
node.left = node.right;
node.right = temp;
}
}
return root;
}
module.exports = {
Mirror : Mirror
}; 
京公网安备 11010502036488号