/**
* 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 tmpnode=root.left;
root.left = root.right;
root.right=tmpnode;
//递归交换当前节点的 左子树
mirrorTree(root.left);
//递归交换当前节点的 右子树
mirrorTree(root.right);
return root;
}
}
参考博客链接: