剑指 Offer 27. 二叉树的镜像
递归
public class Solution {
public TreeNode mirrorTree(TreeNode root) {
// 如果节点不为空
if (root != null) {
// 交换左右子树
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
// 递归调用,分别处理左右子树
mirrorTree(root.left);
mirrorTree(root.right);
}
return root;
}
}