二叉树的镜像

二叉树的镜像_牛客网

https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

方法有二:
其一:先序遍历,第一次经过结点时交换左右孩子
void Mirror(TreeNode *pRoot) {
        if(!pRoot) return;
        swap(pRoot->left,pRoot->right);
        Mirror(pRoot->left);
        Mirror(pRoot->right);
        
    }
其二:后序遍历,最后一次经过结点时交换左右孩子
void Mirror(TreeNode *pRoot) {
        if(!pRoot) return;
        Mirror(pRoot->left);
        Mirror(pRoot->right);
        swap(pRoot->left,pRoot->right);
    }

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 17:17
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务