跳至主要内容

226. Invert Binary Tree

  • Recursion
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root)
{
if (!root) return NULL;

swap(root->left, root->right);

invertTree(root->left);
invertTree(root->right);
return root;
}
};
  • T: O(N)O(N)

  • S: O(H)O(H)

  • Iteration

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root)
{
if (!root) return NULL;
queue<TreeNode*> q;
q.push(root);
while (!q.empty())
{
TreeNode* node = q.front(); q.pop();

swap(node->left, node->right);

if(node->left) q.push(node->left);

if(node->right) q.push(node->right);
}
return root;
}
};
  • T: O(N)O(N)
  • S: O(N)O(N)