# 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
right = self.mirrorTree(root.left)
left = self.mirrorTree(root.right)
root.left = left
root.right = right
return root
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func mirrorTree(root *TreeNode) *TreeNode {
if root == nil {
return root
}
left := mirrorTree(root.Left)
right := mirrorTree(root.Right)
root.Right = left
root.Left = right
return root
}
共以下 36 个回答
时间复杂度O(N)
空间复杂度 O(logN) 最差链式结构O(N)
二叉树镜像
递归
执行用时:0 ms, 在所有 C++ 提交中击败了100.00%的用户
递归 时间O(n),因为遍历了所有结点。 空间O(n),最坏情况是链子状
时间复杂度:O(n)
空间复杂度:O(n)
代码如下
递归
/**中文
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode mirrorTree(TreeNode* root) {
if(!root) return root;
mirrorTree(root -> left);
mirrorTree(root -> right);
TreeNode* cur = new TreeNode(-1);
cur = root -> left;
root -> left = root -> right;
root -> right = cur;
return root;
}
};
Day7:下面是代码
递归
思路:先将要镜像的子树根节点的左节点进行镜像,再将根节点的右节点进行镜像,最后将根节点的左右指针进行交换即可
翻转二叉树
可以先翻转,再递归子树;也可以先递归子树,再翻转
区别只是在于遍历的顺序,贴出来的做法是后序遍历。
七
/**
* 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 tmp = root.left;
root.left = mirrorTree(root.right);
root.right = mirrorTree(tmp);
return root;
}
}
时间:O(n)
空间:O(n)
“`c++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root == NULL){
return NULL;
}
TreeNode* l = mirrorTree(root->left);
TreeNode* r = mirrorTree(root->right);
root->left = r;
root->right = l;
return root;
}
};
“`
时间复杂度:O(n)
空间复杂度:O(n)
c++代码
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root)
{
return rootNULL?NULL:new TreeNode(root->val,mirrorTree(root->right),mirrorTree(root->left));
}
};
”’java
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if(root null){
return null;
}
//交换左右结点
root.left = mirrorTree(root.left);
root.right = mirrorTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
”’
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
空间复杂度:O(n)
// C++实现
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if (root nullptr) {
return nullptr;
}
// 递归到子节点,先镜像子左右树,再往上镜像根节点左右树
TreeNode* left = mirrorTree(root->left);
TreeNode* right = mirrorTree(root->right);
root->left = right;
root->right = left;
return root;
}
};
时间复杂度:O(n)
空间复杂度:O(n)
class Solution {
public TreeNode mirrorTree(TreeNode root) {
//交换左右子树
if(rootnull)
{
return root;
}
mirrorTree(root.left);
mirrorTree(root.right);
TreeNode temp=root.left;
root.left=root.right;
root.right=temp;
return root;
}
时间复杂度O(n)空间复杂度O(n)时间复杂度O(n),空间复杂度O(n)