实现思路:
其实就是先序遍历,依次交换节点的左子树与右子树。使用递归就可以解决,但要注意书写递归的终止条件。
JavaScript代码实现如下:
function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
}
function Mirror(root)
{
return recursion(root)
}
function recursion(node) {
if(!node || (!node.left && !node.right)) {
return node;
}
var temp = node.left;
node.left = recursion(node.right);
node.right = recursion(temp);
return node;
}
module.exports = {
Mirror : Mirror
};
京公网安备 11010502036488号