https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
/**
* 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;
}
TreeNode node = root.left;
root.left = root.right;
root.right = node;
mirrorTree(root.left);
mirrorTree(root.right);
return root;
}
}
2.非递归
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if (root == null){
return root;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();
if(cur.left!=null){
stack.push(cur.left);
}
if(cur.right!=null){
stack.push(cur.right);
}
TreeNode node = cur.left;
cur.left = cur.right;
cur.right = node;
}
return root;
}
}