-
请完成一个函数,输入一颗二叉树,该函数输出它的镜像
题目解读
代码
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot != NULL){
TreeNode *temp = NULL;
temp = pRoot -> left;
pRoot -> left = pRoot -> right;
pRoot -> right = temp;
if(pRoot -> left){
Mirror(pRoot -> left);
}
if(pRoot -> right){
Mirror(pRoot -> right);
}
}
}
};
class Solution {
public:
void Mirror(TreeNode *pRoot) {
if(pRoot == NULL){
// 如果此节点为空,则什么也不做
}
else{
TreeNode* temp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = temp;
Mirror(pRoot->left);
Mirror(pRoot->right);
}
}
};
总结展望