『递归典型题目』题解 | #二叉树的镜像#

二叉树的镜像

http://www.nowcoder.com/practice/a9d0ecbacef9410ca97463e4a5c83be7

递归模板题

/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 *    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pRoot TreeNode类 
     * @return TreeNode类
     */
    TreeNode* Mirror(TreeNode* pRoot) {
        // write code here
        if( nullptr==pRoot ) return nullptr;

        //1、先各自分治去镜像
        Mirror( pRoot->left );
        Mirror( pRoot->right );

        //2.然后才轮到调转我们自己
        TreeNode * temp=pRoot->left;
        pRoot->left=pRoot->right;
        pRoot->right=temp;
        return pRoot;
    }
};
全部评论

相关推荐

03-31 17:40
已编辑
门头沟学院 算法工程师
程序员牛肉:小牛肉来也! 也不要焦虑啦,你第一志愿还没有结束,只是回到人才库(泡大池子等待各个部门挑选)而已。仅仅代表你不符合这个组的用人标准,并不能够说明你在本次暑期实习中没机会加入美团了。 还是平复好心态,不断的复盘,等待下一次面试就好了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务