题目:
Invert a binary tree.
4 / 2 7 / / 1 3 6 9
to
4 / 7 2 / / 9 6 3 1
解析:
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 if(root!=NULL) 14 { 15 TreeNode* tmp=root->left; 16 root->left=root->right; 17 root->right=tmp; 18 invertTree(root->left); 19 invertTree(root->right); 20 } 21 return root; 22 } 23 };