层次遍历
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if(!root) return NULL;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
TreeNode* cur=que.front();
que.pop();
TreeNode* temp=cur->left;
cur->left=cur->right;
cur->right=temp;
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
return root;
}
};