剑指offer-二叉树的镜像

先序遍历,交换子树即可,注意空节点的情况,这种简单题考的就是细节...

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/
class Solution {
public:
    void Mirror(TreeNode *pRoot) {
        if(!pRoot) return;
        if(pRoot->left == NULL && pRoot->right == NULL)
            return;
        swap(pRoot->left, pRoot->right);
        if(pRoot->left)  Mirror(pRoot->left);
        if(pRoot->right) Mirror(pRoot->right);
    }
};
View Code

 

posted @ 2017-10-13 18:29  Deaglepc  阅读(142)  评论(0编辑  收藏  举报