解法一:递归
将源节点的左孩子作为新节点的右孩子,右孩子作为新节点的左孩子,递归完成。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode mirrorNode = new TreeNode(root.val);
if (root.left != null) {
mirrorNode.right = mirrorTree(root.left);
}
if (root.right != null) {
mirrorNode.left = mirrorTree(root.right);
}
return mirrorNode;
}
}