这道题主要考察利用画图解决抽象问题。
具体分析参考剑指offer27.
时间和内存消耗以及源代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void exchangeTree(struct TreeNode* root)
{
if (root->left == NULL)
{
root->left = root->right;
root->right = NULL;
}
else if (root->right == NULL)
{
root->right = root->left;
root->left = NULL;
}
else
{
struct TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
}
return;
}
struct TreeNode* mirrorTree(struct TreeNode* root){
if (root == NULL)
{
return NULL;
}
exchangeTree(root);
mirrorTree(root->left);
mirrorTree(root->right);
return root;
}