递归交换左右树

二叉树的镜像

http://www.nowcoder.com/questionTerminal/564f4c26aa584921bc75623e48ca3011

使用递归的思路:每访问一个结点,就交换其左右树。

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) :
        val(x), left(NULL), right(NULL) {}

};
void Mirror(TreeNode* pRoot) {
    if (pRoot == NULL)
        return ;
    struct TreeNode* t;
    t = pRoot->left;
    pRoot->left = pRoot->right;
    pRoot->right = t;
    Mirror(pRoot->left);
    Mirror(pRoot->right);
}
全部评论

相关推荐

JamesGosling1:同一个公司的实习为什么写三次,就算是不同的小组的话,直接写一段要好点吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务