题目链接: 二叉树的镜像
题意: 操作给定的二叉树, 将其变换为源二叉树的镜像.
题解: 递归大法好啊. 上来交换一波左右节点, 如果有左右子树, 递归交换.
代码:
- /*
- 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 == NULL || (pRoot->left == NULL && pRoot->right == NULL) ) return;
- TreeNode *temp = pRoot->left;
- pRoot->left = pRoot->right;
- pRoot->right = temp;
- if(pRoot->left) Mirror(pRoot->left);
- if(pRoot->right) Mirror(pRoot->right);
- }
- };
来源: http://www.bubuko.com/infodetail-3445663.html