【二叉树专题】剑指 Offer 27. 二叉树的镜像

本问题对应课程链接:剑指offer刷题第六期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 35 个回答

  • YOLO 普通 2024年3月21日 下午12:21
    /*请完成一个函数,输入一个二叉树,该函数输出它的镜像。 */
    class Solution {
        public TreeNode mirrorTree(TreeNode root) {
            if(root == null) 
                return root;
            if(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;
        }
    }
    
    0 赞同 0 条回复
  • kfaebot 普通 2024年3月14日 上午9:47
    /**
     * 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:
        //步骤1:定义返回值和参数
        TreeNode* reverse_tree(TreeNode* node){
            //步骤2:定义终止条件
            if(node==nullptr){
                return nullptr;
            }
    
            //步骤3:定义遍历过程
            TreeNode* tmp=node->left;
            node->left=reverse_tree(node->right);
            node->right=reverse_tree(tmp);
            return node;
        }
        TreeNode* mirrorTree(TreeNode* root) {
            return reverse_tree(root);
        }
    };
    
    0 赞同 0 条回复
  • HU 普通 2024年3月14日 上午9:38
    /**
     * Definition for a binary tree node.
     * function TreeNode(val) {
     *     this.val = val;
     *     this.left = this.right = null;
     * }
     */
    /**
     * @param {TreeNode} root
     * @return {TreeNode}
     */
    var mirrorTree = function(root) {
        // 递归结束条件
        if(root == null || (root.left == null && root.right == null)) {
            return root;
        }
    
        // 递归关系式
        let left = mirrorTree(root.left);
        let right = mirrorTree(root.right);
    
        root.left = right;
        root.right = left;
    
        return root;
    };
    
    0 赞同 0 条回复
  • RunAsOne 普通 2024年3月12日 下午1:36

    第n天打卡

    # 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
    
    
    
    0 赞同 0 条回复
  • Let it be 永久会员 2024年3月11日 下午9:06

    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)

    0 赞同 0 条回复
  • MARS 普通 2024年3月11日 下午7:20

    day7第一题

    function mirrorTree(root: TreeNode | null): TreeNode | null {  //交换左右节点
       if(!root) return null   //终止条件
       let temp = root.left  //将左右节点交换
       root.left = root.right
       root.right = temp
       mirrorTree(root.left)//依次去交换子节点的左右节点,直到为null
       mirrorTree(root.right)
    
       return root  //返回值
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月10日 上午11:04

    “`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;

    }
    

    };

    0 赞同 0 条回复
  • =.= 普通 2024年3月9日 下午11:03

    /**
    * 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;
    }

    public void mirror(TreeNode root){
        if(root == null) return;
    
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
    
        mirror(root.left);
        mirror(root.right);
        return;
    }
    

    }

    时间复杂度O(n),空间复杂度O(n)

    0 赞同 0 条回复
  • 神经蛙 普通 2024年3月9日 下午9:57

    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);

            return root;
    }
    

    };
    时间复杂度O(n),空间复杂度:树高

    0 赞同 0 条回复
  • helloMoon 普通 2024年3月9日 下午9:50

    二叉树的镜像java

    /**
     * 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 || 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;
        }
    }
    
    0 赞同 0 条回复
  • 零 普通 2024年3月9日 下午9:46

    代码:

    
    class Solution {
        public TreeNode mirrorTree(TreeNode root) {
            if (null == root) {
                return null;
            }
            TreeNode node = root.left;
            root.left = mirrorTree(root.right);
            root.right = mirrorTree(node);
            return root;
        }
    }
    
    0 赞同 0 条回复
  • 莫~慢~待 普通 2024年3月9日 下午2:08

    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;
    }
    }
    递归交换左右子节点

    0 赞同 0 条回复
  • 方铎码农 普通 2024年3月9日 下午12:51
    /**
     * 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 || (root.left == null && root.right == null)){
                return root;
            }
    
            TreeNode left = mirrorTree(root.right);
            TreeNode right =  mirrorTree(root.left);
            root.left = left;
            root.right = right;
    
            return root;
        }
    }
    

    时间复杂度O(n),空间复杂度树高

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月8日 下午10:19

    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)

    0 赞同 0 条回复
  •  南木 普通 2024年3月8日 下午9:20

    时间复杂度O(n),空间复杂度树高

    /**
     * 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.right);
            TreeNode right = mirrorTree(root.left);
    
            root.left = left;
            root.right = right;
            return root;
        }
    }
    
    0 赞同 0 条回复
  • Jimmy 普通 2024年3月8日 下午8:01

    时间复杂度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 == NULL){
                return root;
            }
            swap(root->left,root->right);
            mirrorTree(root->left);
            mirrorTree(root->right);
            return root;
        }
    };
    
    0 赞同 0 条回复
  • 叫不醒装睡的人 普通 2024年3月8日 下午6:04
    class Solution {
        // 整个二叉树元素镜像对称交换
        // 用一个临时变量先保存当前节点的左子,将当前节点的右子赋值给当前节点的左子
        // 当前节点的右子存放之前临时变量的元素
        public TreeNode mirrorTree(TreeNode root) {
            if (root == null) {
                return null;
            }
            TreeNode temp = root.left;
            root.left = mirrorTree(root.right);
            root.right = mirrorTree(temp);
            return root;
        }
        // 时间复杂度为 O(n)
        // 空间复杂度为 O(n)
        // 当最坏情况二叉树退化成链表时,空间复杂度为 O(n)
    }
    
    0 赞同 0 条回复
  • 白雪公主倒拔垂杨柳 永久会员 2024年3月8日 下午4:59
    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;
        }
    }
    

    时间复杂度:O(n)
    空间:树的高度

    0 赞同 0 条回复
  • NULL 普通 2024年3月8日 下午4:42

    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;
    }
    }

    0 赞同 0 条回复
  • 一个达不刘 普通 2024年3月8日 下午3:43

    class Solution {
    public:
    TreeNode* mirrorTree(TreeNode* root) {
    if(root NULL) return NULL;

        TreeNode* left =mirrorTree(root->left); //递归解决左子树,并用left接收结果
        TreeNode* right = mirrorTree(root->right);
    
        root->left = right;
        root->right = left;
    
        return root;
    
    }
    

    };
    更进一步的理解了递归三部曲,本次积累经验:递归函数调用,记得找个变量接收一下结果。

    0 赞同 0 条回复
  • 南梁陈庆之 普通 2024年3月8日 下午3:05

    时间复杂度O(N),空间复杂度O(N)

    /**
     * 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 tempNode = root.left;
            root.left = mirrorTree(root.right);
            root.right = mirrorTree(tempNode);
            return root;
        }
    }
    
    0 赞同 0 条回复
  • ChenZhangHangTime 永久会员 2024年3月8日 下午2:50

    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)
    空间:树的高度。

    0 赞同 0 条回复
  • Free 普通 2024年3月8日 上午11:17
    /**
     * 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 == nullptr)
                return nullptr;
            TreeNode* tmp = root->left;
            root->left = mirrorTree(root->right);
            root->right = mirrorTree(tmp);
            return root;
        }
    };
    

    时空复杂度O(n)

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月8日 上午11:12

    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 ;

    0 赞同 0 条回复
  • Thaddeus 永久会员 2024年3月8日 上午10:05

    打卡

    class Solution {
    public:
        TreeNode* mirrorTree(TreeNode* root) {
            if(!root || (!root->left && !root->right)) return root;
            TreeNode* rleft = mirrorTree(root->left);
            TreeNode* rright = mirrorTree(root->right);
            root->left = rright;
            root->right = rleft;
            return root;
        }
    };
    
    0 赞同 0 条回复
  • Yuki 永久会员 2024年3月8日 上午9:20

    class Solution {
    public:
    TreeNode* mirrorTree(TreeNode* root) {
    if(rootNULL)return NULL;

        swap(root->left,root->right);
        root->left=mirrorTree(root->left);
        root->right=mirrorTree(root->right);
    
        return root;
    }
    

    };
    //时间复杂度:O(N),空间复杂度:O(N)

    0 赞同 0 条回复
  • Yox 普通 2024年3月8日 上午4:09

    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)

    0 赞同 0 条回复
  • heathhou 普通 2024年3月7日 下午10:48
    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;
    
        }
    };
    
    0 赞同 0 条回复
  • 机电皮怪 永久会员 2024年3月7日 下午9:51

    思路:先递归到叶子节点,再逐个颠倒左右
    时间复杂度O(n)
    空间复杂度O(n)

    “`C++
    class Solution {
    public:
    TreeNode* mirrorTree(TreeNode* root)
    {
    if(root == nullptr || (root->left == nullptr && root->right == nullptr))
    {
    return root;
    }

        TreeNode* newleft = mirrorTree(root->right);
        TreeNode* newright = mirrorTree(root->left);
    
        root->left = newleft;
        root->right = newright;
    
        return root;
    }
    

    };

    “`

    0 赞同 0 条回复
  • 紫芝 普通 2024年3月7日 下午8:37

    思路:使用递归,先递归镜像以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;
    }
    };
    “`

    0 赞同 0 条回复
  • 言之命至 普通 2024年3月7日 下午8:32
        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;
        }
    

    时间复杂度:O(n)
    空间复杂度:树的高度

    0 赞同 0 条回复
  • 王位詮釋 普通 2024年3月7日 下午7:53

    ···
    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)

    “`

    0 赞同 0 条回复
  • 进取 普通 2024年3月7日 下午7:48

    打卡

    0 赞同 0 条回复
  • Raphtalia. 普通 2024年3月7日 下午7:36

    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)

    0 赞同 0 条回复
  • 云无深雁影 普通 2024年3月7日 下午7:07

    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

    0 赞同 0 条回复

# 回答此问题

后才能回答