思想:从最小的左右子树开始不断交换左右子树直到交换完根节点root的左右子树,然后再return root
/**
* 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||(root.left==null&&root.right==null)) return root;
TreeNode temp = root.right;
//交换
root.right = mirrorTree(root.left);
root.left = mirrorTree(temp);
return root;
}
}