和最近比较火的一条Twitter相关,题目本身其实并不难...递归和非递归代码如下:
/*if(!root)
return root;
TreeNode* lf = root->left;
root->left = invertTree(root->right);
root->right = invertTree(lf);
return root;*/
queue<TreeNode *> q;
if(root)
q.push(root);
while(!q.empty()){
TreeNode *t = q.front();
q.pop();
swap(t->left, t->right);
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
}
return root;