方法一
借用辅助空间
public void Mirror(TreeNode root) {
if(root == null) {
return;
}
ArrayList<TreeNode> list = new ArrayList<>();
list.add(root);
while(!list.isEmpty()) {
TreeNode node = list.remove(0);
TreeNode tmpNode = node.left;
node.left = node.right;
node.right = node.left;
if(node.left!=null) {
list.add(node.left);
}
if(node.right!=null) {
list.add(node.right);
}
}
}
}
方法二
public class Solution {
public void Mirror(TreeNode root) {
if(root == null) {
return;
}
TreeNode tmpNode = root.left;
root.left = root.right;
root.right = tmpNode;
Mirror(root.left);
Mirror(root.right);
}
}
网友评论