题目链接:剑指 Offer 27. 二叉树的镜像
题目描述:
后序遍历,找到最下面,边回溯边交换子节点,可以避免从上向下交换造成的bug
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root==null)return null;
//找到最下面
mirrorTree(root.left);
mirrorTree(root.right);
//交换子节点
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
return root;
}
}