226. Invert Binary Tree

problem

solution

option 1 - backtracking

  • inorder traverse version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
// post-order

root->left = invertTree(root->left);
root->right = invertTree(root->right);

TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;

return root;
}
};
  • postorder traverse version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;

root->left = invertTree(root->left);
root->right = invertTree(root->right);
TreeNode* temp = root->left;
root->left = root->right;
root->right = temp;

return root;
}
};

option 2 - bfs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
queue<TreeNode * >q;
q.push(root);
while(!q.empty()){
int size = q.size();
while(size-- > 0){
TreeNode *p = q.front();
q.pop();
if(p->left) q.push(p->left);
if(p->right) q.push(p->right);
TreeNode *temp = p->left;
p->left = p->right;
p->right = temp;
}
}
return root;
}
};

analysis

  • time complexity O(n)
  • space complexity O(n)