面试题27. 二叉树的镜像
题目:
解答:
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode* mirrorTree(TreeNode* root) 13 { 14 if (NULL == root) 15 { 16 return root; 17 } 18 19 Mirror(root); 20 21 return root; 22 } 23 24 void Mirror(TreeNode *root) 25 { 26 if (NULL == root) 27 { 28 return; 29 } 30 Mirror(root->left); 31 Mirror(root->right); 32 33 TreeNode *tmp = root->left; 34 root->left = root->right; 35 root->right = tmp; 36 } 37 38 };