递归的将二叉树左右交换
交换函数也可以直接写在一起
- Definition for a binary tree node.
- struct TreeNode {
-
int val;
-
TreeNode *left;
-
TreeNode *right;
-
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- };
/
class Solution {
public:
template
void swimp(T& a,T& b)
{
T c=a;
a=b;
b=c;
}
TreeNode mirrorTree(TreeNode* root)
{
if (root == NULL)return NULL;
swimp(root->left,root->right);
root->left=mirrorTree(root->left);
root->right=mirrorTree(root->right);
return root;
}
};