题目链接:二叉树的镜像
题意:操作给定的二叉树,将其变换为源二叉树的镜像。
题解:递归大法好啊。上来交换一波左右节点,如果有左右子树,递归交换。
代码:
1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 };*/ 10 class Solution { 11 public: 12 void Mirror(TreeNode *pRoot) { 13 if(pRoot == NULL || (pRoot->left == NULL && pRoot->right == NULL) ) return; 14 15 TreeNode *temp = pRoot->left; 16 pRoot->left = pRoot->right; 17 pRoot->right = temp; 18 19 if(pRoot->left) Mirror(pRoot->left); 20 if(pRoot->right) Mirror(pRoot->right); 21 } 22 };
原文:https://www.cnblogs.com/Asumi/p/12403538.html