递归结束条件:root为空返回true
递归每层做什么:前序遍历所有节点,并交换left和right节点
递归返回:当前root节点
12345678910111213141516171819202122232425262728293031
/** * 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; } swap(root); mirrorTree(root.left); mirrorTree(root.right); return root; } private void swap(TreeNode root) { TreeNode t = root.left; root.left = root.right; root.right = t; }}