递归 时间复杂度O(n) 空间复杂度O(n)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
if not root:
return None
left = self.mirrorTree(root.left)
right = self.mirrorTree(root.right)
root.right = left
root.left = right
return root
共以下 47 个回答
“`java queue = new LinkedList<>();
从上到下依次翻转即可
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if (root == null) {
return null;
}
Queue
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
}
return root;
}
}
/**
* 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 null;
}
TreeNode left = mirrorTree(root.left);
TreeNode right = mirrorTree(root.right);
root.right = left;
root.left = right;
return root;
}
}
递归
“`java
时间复杂度O(n) 空间复杂度0(n)
class Solution{
public TreeNode mirrorTree(TreeNode root){
if(root == null || (root.left == null&&root.right == null)){
return root;
}
TreeNode left = mirrorTree(root.left);
TreeNode right = mirrorTree(root.right);
root.left=right;
root.right=left;
return root;
}
思路:递归,交换左右子树
java实现
时间复杂度:O(n),空间复杂度 :O(n)
思路:先确定每个部分的功能,然后对递归范围进行缩减,三对最后的最小单位进行分析
【思路】递归对每棵子树做左右子树的swap
【复杂度】时间复杂度O(n),空间复杂度O(1)
C++实现
这段代码是一个二叉树的镜像反转函数。给定一个二叉树的根节点,函数会将该二叉树进行镜像反转,并返回反转后的二叉树的根节点。
代码的步骤如下:
1. 首先定义了一个TreeNode类,用于表示二叉树的节点。节点包含一个值val,以及左右子节点left和right。
2. 然后定义了一个Solution类,其中包含了一个mirrorTree函数,用于实现二叉树的镜像反转。
3. 在mirrorTree函数中,首先判断根节点是否为空。如果为空,则直接返回空节点。
4. 如果根节点不为空,接下来进行镜像反转。通过递归调用mirrorTree函数,分别对根节点的左子节点和右子节点进行镜像反转。
5. 反转的方式是通过交换根节点的左右子节点,即将根节点的左子节点赋值为右子节点,右子节点赋值为左子节点。
6. 最后返回反转后的根节点。
这段代码实现了一个二叉树的镜像反转功能,通过递归的方式对二叉树的每个节点进行处理,将其左右子节点进行交换,从而实现整个二叉树的镜像反转。
/** 时间复杂度O(n) 空间复杂度O(n)
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left TreeNode
* Right *TreeNode
* }
*/
func mirrorTree(root *TreeNode) *TreeNode {
if rootnil{
return nil
}
swap(root)
return root
}
func swap(root *TreeNode) (TreeNode,*TreeNode) {
if root.Left!=nil||root.Right!=nil{
root.Left,root.Right=root.Right,root.Left
}
if root.Left!=nil{
swap(root.Left)
}
if root.Right!=nil{
swap(root.Right)
}
return root.Left,root.Right
}
时间复杂度O(n),空间复杂度O(1)
递归
【时间O(n),空间O(1)】
递归
思路:前序遍历,将每个左右子树进行交换,即可实现镜像翻转
时间复杂度O(n) 空间复杂度O(n)
时间和空间复杂度都是O(N)
dfs
时间:O(n)
空间:O(n)
bfs
时间:O(n)
空间:O(n)
public TreeNode mirrorTree(TreeNode root) {
if(root null || (root.left null && root.right null)){
return root;
}
时间复杂度:o(n),其中n为二叉树节点的数目,会遍历二叉树的每个节点。
空间复杂度:o(n),使用的空间有递归栈的深度
方法一:递归
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
traverse(root);
return root;
}
void traverse(TreeNode* root){
if(rootnullptr){
return;
}
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;
traverse(root->left);
traverse(root->right);
}
};
方法二:分解问题
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(rootnullptr){
return nullptr;
}
TreeNode* lefttree = mirrorTree(root->left);
TreeNode* righttree = mirrorTree(root->right);
root->right = lefttree;
root->left = righttree;
return root;
}
};
时间复杂度O(n),空间复杂度O(n)
时间复杂度O(n)
空间复杂度O(n)
时间复杂度O(N)
空间复杂度O(N)
时间复杂度:O(n)
空间复杂度:树的高度。
时间复杂度:O(N)
空间复杂度:O(N)
c++实现递归
TreeNode* mirrorTree(TreeNode* root) {
if(rootNULL)return root;
stack<TreeNode> st;
st.push(root);
while(!st.empty())
{
TreeNode temp = st.top();
st.pop();
swap(temp->left,temp->right);
if(temp->left!=NULL)st.push(temp->left);
if(temp->right!=NULL)st.push(temp->right);
}
return root;
}
思路:将节点的左右子树的交换,然后再将下一层子树的左右子树进行交换
啊啊啊 递归好像有点懂了。
时间复杂度O(n) 空间复杂度O(n)
dfs,时间复杂度O(n)
时间复杂度为O(N)
空间复杂度为O(N)
复杂度
时间复杂度: $O(n)$
空间复杂度: $O(n)$
Code
时空复杂度:O(n)
1、还是递归三部曲
2、写出方法参数和返回值
3、写出结束条件
4、找出等价关系
时间复杂度:O(n)
空间复杂度:O(n)
自底向上(左右根)方式递归,交换左节点和右节点,递归终点是空节点
时间复杂度O(n) 空间复杂度 O(h)
时间O(1) 空间O(N)
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root NULL) return root;
swap(root->left,root->right);
mirrorTree(root->left);
mirrorTree(root->right);
return root;
}
};
时间复杂度O(n) 空间复杂度树的高度
思路
代码
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:树的高度
复杂度O(n) O(n)
复杂度O(n) O(n)
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root null || (root.left null && root.right null)){
return root;
}
}
时间复杂度:O(n)
空间:树的高度。