The sword refers to the offer 27. The mirror image of the binary tree
Title description
Problem-solving ideas
The main is the exchange root.left
and root.right
advance with temp save one pointer on the line.
class Solution {
//将以root为根节点的树镜像翻转,返回翻转后的根节点
public TreeNode mirrorTree(TreeNode root) {
//base case
if (root == null) return null;
TreeNode temp = mirrorTree(root.left);
root.left = mirrorTree(root.right);
root.right = temp;
return root;
}
}