【LeetCode】226. 翻转二叉树
题目
翻转一棵二叉树。
示例:
输入:
4
/ \
2 7
/ \ / \
1 3 6 9
输出:
4
/ \
7 2
/ \ / \
9 6 3 1
思路一:递归
代码
时间复杂度:O(n)
空间复杂度:O(n)
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root) {
TreeNode *node = root->left;
root->left = root->right;
root->right = node;
root->left = invertTree(root->left);
root->right = invertTree(root->right);
}
return root;
}
}
思路二:迭代
类似深度优先。
代码
时间复杂度:O(n)
空间复杂度:O(n)
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (!root) {
return root;
}
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode *node = q.front();
TreeNode *tmp = node->left;
node->left = node->right;
node->right = tmp;
q.pop();
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
return root;
}
};