操作给定的二叉树,将其变换为源二叉树的镜像。
数据范围:二叉树的节点数
, 二叉树每个节点的值
要求: 空间复杂度
。本题也有原地操作,即空间复杂度
的解法,时间复杂度
比如:
源二叉树
镜像二叉树
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) {
if(pRoot==null)
return pRoot;
TreeNode left=Mirror(pRoot.left);
TreeNode right=Mirror(pRoot.right);
pRoot.left=right;
pRoot.right=left;
return pRoot;
}
} /**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
TreeNode* Mirror(TreeNode* pRoot) {
// 时间复杂度O(N),空间复杂度O(N)
if (pRoot == nullptr) return nullptr;
TreeNode *tmp = pRoot->left;
pRoot->left = Mirror(pRoot->right);
pRoot->right = Mirror(tmp);
return pRoot;
}
}; public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
// 思路 从底层交换
if(pRoot == null){
return null;
}
TreeNode left = Mirror(pRoot.left);
TreeNode right = Mirror(pRoot.right);
pRoot.left = right;
pRoot.right = left;
return pRoot;
}
} //递归
TreeNode* Mirror(TreeNode* pRoot) {
// write code here
if(pRoot==NULL)
return NULL;
TreeNode*p =pRoot->right;
pRoot->right=pRoot->left;
pRoot->left=p;
Mirror(pRoot->left);
Mirror(pRoot->right);
return pRoot;
} //非递归
TreeNode* Mirror(TreeNode* pRoot) {
// write code here
if(pRoot==NULL)
return NULL;
stack<TreeNode*>st;
st.push(pRoot);
while(!st.empty())
{
TreeNode*top = st.top();
st.pop();
TreeNode*p=top->right;
top->right = top->left;
top->left=p;
if(top->right!=NULL)
{
st.push(top->right);
}
if(top->left!=NULL)
{
st.push(top->left);
}
}
return pRoot;
}
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param pRoot TreeNode类
# @return TreeNode类
#
class Solution:
def Mirror(self , pRoot: TreeNode) -> TreeNode:
# write code here
if not pRoot:return
tmp=self.Mirror(pRoot.left)
pRoot.left=self.Mirror(pRoot.right)
pRoot.right=tmp
return pRoot
public TreeNode Mirror (TreeNode pRoot) {
//叶子节点都为null或有一个为null都可以处理。
//因为pRoot不为null,没有问题。
if(pRoot != 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类
*/
public TreeNode Mirror (TreeNode pRoot) {
// write code here
//使用递归
//递归的结束条件是,当前节点为空,或者是交换完成
//所谓交换,那必然就是三步法
if(pRoot==null) return null;
TreeNode tmp = pRoot.left;
pRoot.left=Mirror(pRoot.right);
pRoot.right=Mirror(tmp);
return pRoot;
}
} class Solution: def Mirror(self , pRoot ): if not pRoot: return pRoot q = [] #队列 q.append(pRoot) root = pRoot while q: pRoot=q.pop(0) pRoot.left,pRoot.right=pRoot.right,pRoot.left if pRoot.left: q.append(pRoot.left) if pRoot.right: q.append(pRoot.right) return root
function Mirror( pRoot ) {
// write code here
if(pRoot == null){
return pRoot;
}
// var temp = pRoot.left;
// pRoot.left = pRoot.right;
// pRoot.right = temp;
[pRoot.left,pRoot.right] = [pRoot.right,pRoot.left];
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
} public TreeNode Mirror (TreeNode pRoot) {
// write code here
if(pRoot==null)return null; //边界处理
//左右 对称当前左右子节点
if(pRoot.left!=null || pRoot.right!=null){
TreeNode temp =pRoot.left;
pRoot.left =pRoot.right ;
pRoot.right =temp;
//对子结构进行反转
Mirror(pRoot.left);
Mirror(pRoot.right);
}
return pRoot;
}
##采用递归思想## 1.先处理根节点。若根节点为空,或为单个节点,则直接返回。否则交换左右节点 2.处理根节点的左子树 3.处理根节点的右子树
import java.util.*;
public class Solution {
public TreeNode Mirror (TreeNode pRoot) {
if(pRoot==null)
return pRoot;
if(pRoot.left==null && pRoot.right==null)
return pRoot;
//处理根节点,交换左右节点
TreeNode temp=pRoot.left;
pRoot.left=pRoot.right;
pRoot.right=temp;
//处理左子树
Mirror(pRoot.left);
//处理右子树
Mirror(pRoot.right);
return pRoot;
}
}
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param pRoot TreeNode类 # @return TreeNode类 # class Solution: def Mirror(self , pRoot ): if pRoot: right = self.Mirror(pRoot.left) left = self.Mirror(pRoot.right) pRoot.right = right pRoot.left = left return pRoot # write code here
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
struct TreeNode* Mirror(struct TreeNode* pRoot ) {
if (pRoot == NULL) {
return NULL;
}
struct TreeNode* tmp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = tmp;
Mirror(pRoot->left);
Mirror(pRoot->right);
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 pRoot;
}
//如果只有根节点,直接返回根节点
if(pRoot.left==null && pRoot.right == null) return pRoot;
//交换结点
TreeNode temp = pRoot.right;
pRoot.right = pRoot.left;
pRoot.left = temp;
//进入递归
Mirror(pRoot.left);
Mirror(pRoot.right);
return pRoot;
}
}