递归算法
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* invertTree(TreeNode* root) { 13 invertNode(root); 14 return root; 15 } 16 void invertNode(TreeNode * temp) 17 { 18 if(temp!=NULL) 19 { 20 TreeNode* left=temp->left; 21 TreeNode* right=temp->right; 22 temp->left=right; 23 temp->right=left; 24 invertNode(left); 25 invertNode(right); 26 } 27 } 28 };