/**
* 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 root;
}
overturn(root);
return root;
}
private void overturn(TreeNode node) {
TreeNode t = null;
// 翻转
if (node.left != null || node.right != null) {
t = node.left;
node.left = node.right;
node.right = t;
}
// 前进
if (node.left != null) {
overturn(node.left);
}
if (node.right != null) {
overturn(node.right);
}
}
}