跳转至

226.Invert Binary Tree

Tags: Easy Tree

Links: https://leetcode.com/problems/invert-binary-tree/


Invert a binary tree.

Example:

Input:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

Output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

Trivia: This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.


/**
 * 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 nullptr;

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

        return root;
    }
};
/**
 * 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 nullptr;

        queue<TreeNode *> q;
        q.push(root);
        while (!q.empty()) {
            TreeNode *p = q.front();
            q.pop();
            TreeNode * tmp = p -> left;
            p -> left = p -> right;
            p -> right = tmp;
            if (p -> left) q.push(p -> left);
            if (p -> right) q.push(p -> right);
        }

        return root;
    }
};