# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def mirrorTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root or (not root.left and not root.right):
return root
left = self.mirrorTree(root.left)
right = self.mirrorTree(root.right)
root.left = right
root.right = left
return root
共以下 35 个回答
第n天打卡
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root null) return null;
TreeNode tmp = root.left;
root.left = mirrorTree(root.right);
root.right = mirrorTree(tmp);
return root;
}
}时间空间复杂度O(n)
day7第一题
“`c++
时间复杂度O(N),空间复杂度O(1)
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(!root)
return NULL;
TreeNode* p=root->left;
root->left=root->right;
root->right=p;
mirrorTree(root->left);
mirrorTree(root->right);
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 root;
mirror(root);
return root;
}
}
时间复杂度O(n),空间复杂度O(n)
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root NULL) return root;
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
TreeNode *left = mirrorTree(root->left);
TreeNode *right = mirrorTree(root->right);
};
时间复杂度O(n),空间复杂度:树高
二叉树的镜像java
代码:
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(rootnull)
return null;
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
mirrorTree(root.left);
mirrorTree(root.right);
return root;
}
}
递归交换左右子节点
时间复杂度O(n),空间复杂度树高
java
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root null){
return null;
}
TreeNode leftRoot = mirrorTree(root.right);
TreeNode rightRoot = mirrorTree(root.left);
root.left = leftRoot;
root.right = rightRoot;
return root;
}
}
时间复杂度 O(N)
空间复杂度 O(N)
时间复杂度O(n),空间复杂度树高
时间复杂度O(n),空间复杂度最坏情况下为O(n)
时间复杂度:O(n)
空间:树的高度
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root null)return null; // 递归出口
TreeNode left = mirrorTree(root.left);
TreeNode right = mirrorTree(root.right);
root.left = right;
root.right = left;
return root;
}
}
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root NULL) return NULL;
};
更进一步的理解了递归三部曲,本次积累经验:递归函数调用,记得找个变量接收一下结果。
时间复杂度O(N),空间复杂度O(N)
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root null || (root.left null && root.right null)){
return root;
}
}
Java
时间复杂度:O(n)
空间:树的高度。
时空复杂度O(n)
public TreeNode mirrorTree(TreeNode root) {
if(rootnull) return null;
TreeNode tmp =root.left;
root.left=mirrorTree(root.right);
root.right=mirrorTree(tmp);
return root;
}
思路:
终止条件: 当节点 root 为空时(即越过叶节点),则返回 null
递推工作:
初始化节点 tmp ,用于暂存 root 的左子节点;
开启递归 右子节点 mirrorTree(root.right) ,并将返回值作为 root 的 左子节点 。
开启递归 左子节点 mirrorTree(tmp) ,并将返回值作为 root 的 右子节点 。
返回值: 返回当前节点 root ;
打卡
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(rootNULL)return NULL;
};
//时间复杂度: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
root.left,root.right =self.mirrorTree(root.right),self.mirrorTree(root.left)
return root
双o(n)
思路:先递归到叶子节点,再逐个颠倒左右
时间复杂度O(n)
空间复杂度O(n)
“`C++
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root)
{
if(root == nullptr || (root->left == nullptr && root->right == nullptr))
{
return root;
}
};
“`
思路:使用递归,先递归镜像以root的左右子节点为父节点的二叉树,然后交换root的左右子节点,返回root。递归的退出条件为如果root为空,则返回root。
“`c++
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if (root == nullptr) return root;
TreeNode *tmp = mirrorTree(root->left);
root->left = mirrorTree(root->right);
root->right = tmp;
return root;
}
};
“`
时间复杂度:O(n)
空间复杂度:树的高度
···
java
class Solution {
public TreeNode mirrorTree(TreeNode root) {
reverse(root);
return root;
}
public void reverse(TreeNode root){
if(rootnull){
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
reverse(root.right);
reverse(root.left);
}
}
思路:递归实现,时间复杂度:需要遍历树的所有节点,O(n)
空间复杂度的:没有使用额外空间O(1)
“`
打卡
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(!root) return nullptr;
TreeNode* l = mirrorTree(root->left);
TreeNode* r = mirrorTree(root->right);
root->left = r;
root->right = l;
return root;
}
};
时间复杂度:O(n)空间复杂度O(logn)
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root null)
return null;
mirrorTree(root.left);
mirrorTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
//递归实现 时空复杂度均为On