操作给定的二叉树,将其变换为源二叉树的镜像。
数据范围:二叉树的节点数
, 二叉树每个节点的值
要求: 空间复杂度
。本题也有原地操作,即空间复杂度
的解法,时间复杂度
比如:
源二叉树
镜像二叉树
public TreeNode Mirror (TreeNode pRoot) {
if(pRoot == null) return null;
TreeNode t1 = Mirror(pRoot.left);
TreeNode t2 = Mirror(pRoot.right);
pRoot.left = t2;
pRoot.right = t1;
return pRoot;
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
return process(pRoot);
}
public TreeNode process(TreeNode pRoot) {
// 递归出口
if (pRoot == null) {
return null;
}
// 得到左右子树调整好的根结点
TreeNode leftRoot = process(pRoot.left);
TreeNode rightRoot = process(pRoot.right);
// 将本根结点的子树也调整好
pRoot.left = rightRoot;
pRoot.right = leftRoot;
// 返回本根结点
return pRoot;
}
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
if (pRoot != null) {
TreeNode left = pRoot.left;
pRoot.left = Mirror(pRoot.right);
pRoot.right = Mirror(left);
return pRoot;
} else {
return pRoot;
}
}
} public TreeNode Mirror (TreeNode pRoot) {
// write code here
if(pRoot==null){
return null;
}
TreeNode temp=pRoot.left;
pRoot.left=Mirror(pRoot.right);
pRoot.right=Mirror(temp);
return pRoot;
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
if(pRoot==null) return null;
TreeNode temp = pRoot.left;
pRoot.left = pRoot.right;
pRoot.right = temp;
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
}
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
TreeNode t;
public TreeNode Mirror (TreeNode pRoot) {
//只需要每次递归时把该节点的左右节点交换位置即可
a(pRoot);
return pRoot;
}
public void a(TreeNode root) {
if(root==null)
return;
t=root.left;
root.left=root.right;
root.right=t;
a(root.left);
a(root.right);
}
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
if(pRoot==null||(pRoot.left==null&&pRoot.right==null)) {
return pRoot;
}
Mirror(pRoot.left);
TreeNode tmp=pRoot.left;
pRoot.left=pRoot.right;
pRoot.right=tmp;
Mirror(pRoot.left);//注意因为是把左右子树进行的交换,所以这里往下递归的还是left
return pRoot;
}
} public TreeNode Mirror (TreeNode pRoot) {
//带swap的先序遍历
if(pRoot==null) return pRoot;
build(pRoot);
return pRoot;
}
public void build(TreeNode pRoot){
if(pRoot==null) return;
//先序遍历
TreeNode tmp=pRoot.left;
pRoot.left=pRoot.right;
pRoot.right=tmp;
build(pRoot.left);
build(pRoot.right);
} import java.util.*;
public class Solution {
public TreeNode Mirror (TreeNode root) {
if (root == null) return null;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (! queue.isEmpty()) {
TreeNode curr = queue.poll();
if (curr.left != null) queue.offer(curr.left);
if (curr.right != null) queue.offer(curr.right);
TreeNode temp = curr.left;
curr.left = curr.right;
curr.right = temp;
}
return root;
}
} 使用栈 O(H)import java.util.*;
public class Solution {
public TreeNode Mirror (TreeNode root) {
if (root == null) return null;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (! stack.isEmpty()) {
TreeNode node = stack.pop(), temp = null;
temp = node.left;
node.left = node.right;
node.right = temp;
if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
}
return root;
}
} import java.util.*;
public class Solution {
public TreeNode Mirror (TreeNode root) {
if(root == null) {
return null;
}
preOrder(root);
return root;
}
private void preOrder(TreeNode root) {
if(root == null) {
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
// 递归
preOrder(root.left);
preOrder(root.right);
}
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
return exchange(pRoot);
}
public TreeNode exchange(TreeNode root){
//如果根节点为空或者只有根节点,则直接返回
if(root==null || (root.left==null && root.right==null)){
return root;
}
//创建一个临时节点来更换左右节点
TreeNode temp=new TreeNode(0);
temp=root.left;
root.left=root.right;
root.right=temp;
//更换完毕后,递归调用更换函数,逐层递归更换
exchange(root.left);
exchange(root.right);
return root;
}
} import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
doMirror(pRoot);
return pRoot;
}
private void doMirror(TreeNode root) {
if (root == null) {
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
doMirror(root.left);
doMirror(root.right);
}
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// 根节点为null,或者根节点没有子树,则返回root
if(pRoot == null || (pRoot.left ==null && pRoot.right == null)) return pRoot;
// 递归进行镜像,每次递归都是树本身的子节点对调
Mirror (pRoot.left, pRoot.right, pRoot);
// 返回root
return pRoot;
}
public void Mirror (TreeNode left, TreeNode right, TreeNode pre) {
// 都为空时 不需要镜像
if(left == null && right == null) return;
// 左右节点互相交换
pre.left = right;
pre.right = left;
// left不为空,则向左子树递归将其子节点镜像
if(left != null) Mirror (left.left, left.right, left);
// right不为空,则向右子树 将其子节点镜像
if(right != null) Mirror (right.left, right.right, right);
}
}