首页 > 试题广场 >

二叉树的镜像

[编程题]二叉树的镜像
  • 热度指数:133762 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
作给定的二叉树,将其变换为源二叉树的镜像。
数据范围:二叉树的节点数 , 二叉树每个节点的值
要求: 空间复杂度 。本题也有原地操作,即空间复杂度 的解法,时间复杂度

比如:
源二叉树
镜像二叉树

示例1

输入

{8,6,10,5,7,9,11}

输出

{8,10,6,11,9,7,5}

说明

如题面所示    
示例2

输入

{}

输出

{}

说明:本题目包含复杂数据结构TreeNode,点此查看相关信息
struct TreeNode* Mirror(struct TreeNode* pRoot ) {
    struct TreeNode *LeftTreeNode, *RightTreeNode;
    if(pRoot==NULL)
        return NULL;
    LeftTreeNode = Mirror(pRoot->left);
    RightTreeNode = Mirror(pRoot->right);
    pRoot->right = LeftTreeNode;
    pRoot->left = RightTreeNode;
    return pRoot;
}

编辑于 2024-03-16 17:05:08 回复(0)
struct TreeNode* Mirror(struct TreeNode* pRoot ) {
    // write code here
    if (pRoot==NULL) {
        return NULL;
    }
    struct TreeNode* tmp;
    tmp=pRoot->right;
    pRoot->right=Mirror(pRoot->left);
    pRoot->left=Mirror(tmp);
    return pRoot;
}
发表于 2024-01-04 23:03:55 回复(0)
 

struct TreeNode* Mirror(struct TreeNode* pRoot ) 
{
    // write code here
    //判断结点是否为空
    if(!pRoot)
    {
        return NULL;
    }
    //交换左右子结点
    struct TreeNode*tmp=NULL;
    tmp=pRoot->left;
    pRoot->left=pRoot->right;
    pRoot->right=tmp;
    //递归左右子树
    Mirror(pRoot->left);
    Mirror(pRoot->right);
    //返回交换后的根节点
    return pRoot;
}


发表于 2023-11-17 19:31:05 回复(0)
/**
 * struct TreeNode {
 *  int val;
 *  struct TreeNode *left;
 *  struct TreeNode *right;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param pRoot TreeNode类
 * @return TreeNode类
 */
struct TreeNode* Mirror(struct TreeNode* pRoot )
{
    // write code here
    if(!pRoot)
    {
        return NULL;
    }
    struct TreeNode*temp=pRoot->left;
    pRoot->left=pRoot->right;
    pRoot->right=temp;
    Mirror(pRoot->left);
    Mirror(pRoot->right);

    return pRoot;
}
发表于 2023-07-09 12:39:40 回复(0)

左右根子树互换即可,控件复杂度O(1),时间复杂度O(n)

struct TreeNode* Mirror(struct TreeNode* pRoot ) {
    // write code here
    if(pRoot==NULL) return NULL;
    struct TreeNode* tmp=NULL;
    tmp = pRoot->left;
    pRoot->left=pRoot->right;
    pRoot->right=tmp;
    if(pRoot->left!=NULL) Mirror(pRoot->left);
    if(pRoot->right!=NULL) Mirror(pRoot->right);
    return pRoot;
}
发表于 2022-11-14 17:49:33 回复(0)
struct TreeNode* Mirror(struct TreeNode* pRoot ) {
    // write code here
    if (!pRoot) {
        return pRoot;
    }
    
    struct TreeNode* tmp = pRoot->right;
    pRoot->right = Mirror(pRoot->left);
    pRoot->left = Mirror(tmp);
    
    return pRoot;
}
发表于 2022-02-24 23:24:35 回复(0)
/**
 * struct TreeNode {
 * int val;
 * struct TreeNode *left;
 * struct TreeNode *right;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 *
 * @param pRoot TreeNode类
 * @return TreeNode类
 */
struct TreeNode* Mirror(struct TreeNode* pRoot ) {
    // write code here
    if(pRoot == NULL) return NULL;
    struct TreeNode *Extmp = pRoot->left;
    pRoot->left = pRoot->right;
    pRoot->right = Extmp;
   
    Mirror(pRoot->left);
    Mirror(pRoot->right);
   
    return pRoot;
}
发表于 2021-08-22 09:52:15 回复(0)
struct TreeNode* Mirror(struct TreeNode* pRoot ) {
    // write code here
    if(pRoot == NULL)
        return pRoot;
    struct TreeNode* tmp = NULL;
    tmp = pRoot->left;
    pRoot->left = pRoot->right;
    pRoot->right = tmp;
    
    Mirror(pRoot->left);
    Mirror(pRoot->right);
    
    return pRoot;
}

发表于 2021-07-27 11:35:18 回复(0)